Re: Complex Query



Uleric wrote:
I am converting a date to a variable, then stripping out unwanted
hyphens, making sure there is a 0 in the month/day, and chomping the
year to the last 2 digits. The end result is a 6 digit integer that I
can use in my SQL.

05-31-2006 changes to 053106

Everything works except chomping the year to the last 2 digits. Substr
would normally work, but my results are ommiting the substring
completely.

BEGIN
RIGHT('00' + CAST(DATEPART(mm, @date) as varchar(2)), 2 ) +
RIGHT('00' + CAST(DATEPART(dd, @date) as varchar(2)), 2 ) +
SUBSTRING(CAST(DATEPART(yyyy, @date) as varchar(2)), -2, 2)

Any clues? Corrections?

SELECT REPLACE(CONVERT(CHAR(8),@date,1),'/','') AS dt;

dt



----------
053106

That's a very poor format in which to store or manipulate a date. It's
ambiguous, hard to read, won't sort chronologically and it loses the
century. You may remember a few years ago the world spent billions of
dollars correcting errors like this one.

My advice is that you store dates as dates and where that isn't
possible use standard and well-defined formats like the ISO formats.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

.



Relevant Pages

  • Re: Complex Query
    ... The date is a minor component to a barcode for a manufacturing process. ... Everything works except chomping the year to the last 2 digits. ... possible use standard and well-defined formats like the ISO formats. ... State what version of SQL Server you are using and specify the content ...
    (comp.databases.ms-sqlserver)
  • Re: creating a an automatically incrementing, formatted reference number
    ... You still don't need a command button. ... LabNumber, just type over it. ... that formats what you just entered to appear the way you want. ... use for the lab number is 00-0000 the first two digits being the last ...
    (microsoft.public.access.modulesdaovba)
  • Re: Mergefield formatting codes seem to combine digits in numeric data
    ... the problem /may/ be to do with the money type. ... Something worth trying is to use a cast or convert function in a SQL Server ... If no formatting fields are supplied in the MergeField, ... Instead Word seems to combine digits. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: simple math question
    ... The issue is that SQL Server thinks it is dividing two integers. ... operands are integers, then integer division is performed, then any ... can carry decimal digits might work for you. ...
    (microsoft.public.sqlserver.programming)
  • Re: Multiplying numeric(19,4) Values
    ... The reason for this is how SQL Server handles precision for numeric ... The ultimate goal is not to lose any precision. ... and the first has 5 digits before the decimal place and the ... is where the setting of NUMERIC_ROUNDABORT and ARITHABORT come in. ...
    (microsoft.public.sqlserver.programming)