SQL Server - converting 13 digit numeric to datetime

前端 未结 3 1751
一向
一向 2021-01-19 07:52

How can I convert a 13 digit numeric like 1314637182953 to datetime in SQL Server? Both casting and converting directly give an arithmetic overflow error!

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 08:09

    We are far enough past the epoch (1/1/1970) that you may get an overflow error with the above code.
    It can be fixed easily by casting the field in question as a BIGINT data type before dividing by 1000 as below:

    DATEADD(SECOND, CAST(START_DATE as BIGINT)/1000 ,'1970/1/1')
    

提交回复
热议问题