3

I have a CSV file that has dates written like this: 250909,240909 and they get stored in a SQL Server database as varchars.

How do I get it to convert them into dates so that it gets displayed like this: 25/09/09, using the dd/mm/yyyy format?

At the moment I can get it to convert, but it converts it like this: 2025/09/09, meaning it is using the yyyy/mm/dd format which is wrong, seeing that the first column is the day and second the month and third the year.

2
  • 6
    Store the dates as DATE. Then, when you want the output - in whatever format - format the output, either in SQL-Server or in the application. – ypercubeᵀᴹ Aug 4 '14 at 9:24
  • SQL Server 2008+? – Aaron Bertrand Aug 4 '14 at 16:21
5

I understand your problem to be how to successfully convert the string into a DATE value. An undelimited string of integers is assumed to be Year-Month-Day order. (And, of course, I agree with the comments that dates should be stored in DATE data types in the database.)

Reviewing the MSDN CONVERT documentation does not show a built-in conversion for your string, but it is easy to work around.

http://msdn.microsoft.com/en-us/library/ms187928.aspx -- CAST and CONVERT

I changed the month to 8 to make it easier to double check. Using the CONVERT style option 3, you can do the following:

DECLARE @String VARCHAR(10);
DECLARE @DateValue DATE;
SET @String = '250809'; 

-- Convert your undelimited string DDMMYY into a DATE
-- First: Add / between the string parts.
SET @STRING = SUBSTRING(@String,1,2)+'/'+
     SUBSTRING(@String,3,2)+'/'+SUBSTRING(@String,5,2);
-- Second: Convert using STYLE 3 to get DD/MM/YY interpretation
SELECT @DateValue = CONVERT(Date, @String, 3);

-- Using the DATE 
-- Select the value in default Year-Month-Day
SELECT @DateValue AS DefaultFormat;    
-- Select the value formatted as dd/mm/yy
SELECT CONVERT(VARCHAR(20),@DateValue,3) AS [DD/MM/YY];

The results of the last two selects are:

DefaultFormat
-------------
2009-08-25

DD/MM/YY
--------
25/08/09

To get your DATE formatted in the way you want it, you have to insert the '/' delimiters then use the STYLE 3 in converting from the string to the DATE. (I am sure that there are other workarounds and conversion styles that would work as well.)

Likewise when displaying the DATE as you desire, you need to use STYLE 3

3
  • You can replace the triple substring with set @string = stuff(stuff(@string,5,0,'/'),3,0,'/') to make the code a bit easier to read. MSDN: msdn.microsoft.com/en-us/library/ms188043(v=sql.105).aspx – Pieter Geerkens Aug 4 '14 at 13:56
  • hey thanks RLF anyway instead of having the '250809' i can use the column that its stored in eg @string = enddatecol ? – Ruwald Lichtenstein Aug 4 '14 at 22:47
  • If I understand you, then I think the answer is 'Yes'. I just used the @String to demonstrate the conversions you might need to make. (But a more standard date format could prevent future confusion, if that would be an acceptable format.) – RLF Aug 5 '14 at 1:07
3

Another approach is to cast it directly to a date in SQL Server 2008 or above, then store it that way as @ypercube commented above. Assuming 2000 <= all expected years <= 2099:

DECLARE @d CHAR(6) = '250909';
SELECT DATEFROMPARTS('20'+RIGHT(@d,2),SUBSTRING(@d,3,2),LEFT(@d,2));

You may need to do things a little differently if you could have 250999 etc, then you would need some way to indicate whether that's 1999 or 2099, for example. This also doesn't handle validation (like the other answer, it will choke on values like 252525).

When you want to display the date, then format it at the display/presentation layer, but store it correctly in the database. I still question whether it is actually useful to display as ambiguous formats like 25/09/2009 - for that date specifically it's clearly September 25th, but are you sure your entire audience will always get 07/08/2009 correctly? While I'm in the USA that's July 8th, but last week I was in Canada, and I would expect that to be August 7th. Output formats like 2009-07-08 are much clearer and less prone to misinterpretation; even better would be July 8th, 2009 - but then that opens the door to folks using a different language. All that said, these formats can be completely controlled by the client application (C# has very powerful formatting functions), and shouldn't dictate how you actually store the data in the database. They should be stored as dates because you get automatic validation, all of the date/time functionality, etc. Stop storing dates as strings (and maybe even try to get the CSV to contain more reliable literal formats, like YYYYMMDD). Some useful reading perhaps:

1
  • Agree that 6-digit dates are unclear and better avoided. – RLF Aug 4 '14 at 17:16
-2

You can use this method to convert your date to this format : dd/mm/yyyy.

CONVERT(varchar(10), CAST("datefield" as date), 103) as dd/mm/yyyy
1
  • 2
    This shows how to take a date value and get a "dd/mm/yyyy" string out of it. The OP is trying to take a "ddmmyy" string and correctly convert it to a date value. – RDFozz Aug 17 '18 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.