SQL Server datetime to bigint (epoch) overflow

后端 未结 4 1992
春和景丽
春和景丽 2020-12-01 19:15

I have a \'datetime\' column with value 2013-03-22 15:19:02.000

I need to convert this value into epoch time and store it in a \'bigint\' field

The actual e

4条回答
  •  日久生厌
    2020-12-01 19:50

    When tried to get exact milliseconds we get the overflow exception. we can get the values till seconds and multiply with 1000.

    This is equivalent to new Date().getTime() in javascript:

    Use the below statement to get the time in seconds.

    SELECT cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint)
    

    Use the below statement to get the time in milliseconds.

    SELECT cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint) * 1000
    

    convert epoch to human readable date time using below statement:

    select DATEADD(s, 1481300537, '1970-01-01 00:00:00')
    

提交回复
热议问题