Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem With Case

Author  Topic 

seanmt
Starting Member

7 Posts

Posted - 2010-05-12 : 11:14:56
I am trying to convert dates (stored as nvarchar) into datetime but I'm having problems due to different date formats used to store the data currently.

The dates are currently either stored as..

Null
dd/mm/yy (8 chars)
dd/mm/yyyy (10 chars)
d/mm/yyyy (9 chars)

The code I have at the moment can convert the 8 and 10 char length dates but I don't know how to use nested CASE statements if it's possible to conver the 9 char version.

Current code:

SELECT
CASE WHEN LEN(c.DatePurchased) = 8
THEN CONVERT(datetime, LEFT(c.DatePurchased,6) + '20' + RIGHT(c.DatePurchased,2) , 103)
ELSE CONVERT(datetime, ISNULL(c.DatePurchased,'21/03/2002'), 103)
END AS WhenAdded,
FROM
[CD Catalogue] c

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-12 : 11:22:23
just use like

CASE
WHEN
ELSE CASE
WHEN
ELSE
END

END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

seanmt
Starting Member

7 Posts

Posted - 2010-05-12 : 11:40:17
Ah yes thank you. I wasn't sure if it was possible to nest them.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-12 : 11:42:36
you can as i showed
.
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -