SQL: Convert GMT in seconds to DateTime
I have a table with two columns of GMT time in seconds, and offset in minutes like this:
SELECT TOP 1 StartTime, OffSet FROM MyTable;
1247242537 -420 -- as example
What SQL function I can use to convert it to a datetime in yyyy-MM-dd hh:mm:ss as TimeStamp? T开发者_运维百科he SQL I am using is SQL Server 2005.
This should do it, assuming you Epoch date is 1/1/1970 (usually for GMT seconds, but you will need to confirm)
select dateAdd(ss,1247242537+(-420*60),'1/1/1970')
== > 2009-07-10 09:15:37.000
For you code, use
select DATEADD(ss,StartTime+(Offset*60),'1/1/1970') as TheTime FROM myTable
Be sure you find some test cases to make sure the Epoch date is as expected. Basically, the GMT time is number of seconds past 1/1/1970 12:00am, so DateAdd handles that part. The offset is the number of minutes different from GMT, so multiply that by 60 to get seconds and adjust your value to DateAdd accordingly
精彩评论