June 15, 2011 at 12:35 pm
A developer needs to convert a string 'Thu Aug 5 12:14:49 GMT-0400 2010' into a date format. Does anyone have any suggestions on how this can be done?
June 15, 2011 at 12:58 pm
Something like this?
DECLARE @t VARCHAR(40)
SET @t='Thu Aug 5 12:14:49 GMT-0400 2010'
SELECT CAST(LEFT(STUFF(@t,1,4,''),6) + ' ' + RIGHT(@t,4) + ' '+ LEFT(RIGHT(@t,22),8) AS DATETIME)
As a side note: the time zone information is ignored assuming it is the same as the system time offset.
Edit: the concept is rather simple:
1) remove the weekday, since it's irrelevant
2) move the year to the proper position (after the day) leading to "mon dd yyyy"
3) extract the time information and add it to the string leading to "mon dd yyyy hh:mm:ss"
June 15, 2011 at 1:06 pm
Perfect! That worked. Thank you so much.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply