Convert unix epoch timestamp to TSQL datetime

后端 未结 2 1562
[愿得一人]
[愿得一人] 2020-12-06 11:57

I have found only one similar question but for MySQL.

I was working on a web service and had to query the database (MS SQL server). Since I couldn\'t get the right

相关标签:
2条回答
  • 2020-12-06 12:26

    This should work perfect for those long epoch times.

    SELECT DATEADD(SECOND, 1359016610667 / 1000, '19700101 00:00')
    
    0 讨论(0)
  • 2020-12-06 12:31

    Easy, first add whole days, then add the remaining ms. There are 86,400,000 milliseconds in a day.

    declare @unixTS bigint
    set @unixTS = 1359016610667
    
    
    select dateadd(ms, @unixTS%(3600*24*1000), 
        dateadd(day, @unixTS/(3600*24*1000), '1970-01-01 00:00:00.0')
    )
    

    The result is 2013-01-24 08:36:50.667

    0 讨论(0)
提交回复
热议问题