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!
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')
Assuming it's UNIX time in milliseconds, try this:
DATEADD(SECOND, START_DATE/1000 ,'1970/1/1')
For the number from your post, SQL-2008 returns 2011-08-29 16:59:42.000
If you don't want to chop off the miliseconds:
CAST(DATEADD(ms, CAST(RIGHT(START_DATE,3) AS smallint), DATEADD(s, START_DATE / 1000, '1970-01-01')) AS datetime2(3))