views:

2298

answers:

3

Is there a simple way to convert a date stored in a varchar in the format mon-yy (e.g. "Feb-09") to a datetime which will convert any invalid values to null.

I'm currently using string manipulation combined with a case statement but it's rather cumbersome.

A: 
CONVERT( DATETIME, "Feb-09", 64, 7 )
Jason Cohen
That doesn't seem to work for me :( I've tried the following: select convert(datetime, MyFieldName, 64, 7) from MyTable
mdresser
+5  A: 

For valid values you can just put a day on it and it's a complete date:

convert(datetime,'01-' + 'Feb-09',120)
Guffa
Perfect! Nice and simple, just how I like it!
mdresser
+2  A: 

This will do it.

SELECT convert(datetime,'01-' + 'Feb-09',120)
John Sansom
....a bit slow off the mark.
John Sansom