I have a table in SQL Server 2008 that stores date/time values as varchar(max) (don’t ask). I’ve been tasked with converting these values from PST to GMT. The values are in one of two formats in each row:
Mar 12 2009 4:00PM
OR
3/12/2009 4:00:00PM
How can I, using T-SQL, determine which format the date is in (I’m thinking maybe check to see if it starts with a character or a numeric value) then convert that to a SQL datetime variable so that I can use DATEADD to add hours to it?
I’ve already written the T-SQL to create a cursor and loop through the rows, I’m just not sure how to detect which format it’s in and then convert to DATETIME. I’m sure could probably hack it, but I figured the folks here at SO could provide me with a CORRECT way to do it.
you can use the
convertfunction which will work for both the scenariosthen you can format it as you like