Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSSQL - Convert milliseconds since 1970 to datetime2

Consider the following query (in MSSQL 2008):

SELECT dateModified FROM SomeTable;

This returns floats in javascript format (milliseconds since 1970):

dateModified 
============ 
1301598290687 
1071003581343 
1311951478593

How can I convert this to a datetime2 right in the select?

like image 442
Luke Belbina Avatar asked Jan 15 '23 07:01

Luke Belbina


1 Answers

Using the formula from @Mikeal Eriksson's answer here.

I would convert the float to a bigint and then create the datetime:

select 
  DATEADD(MILLISECOND, 
          cast(dateModified as bigint) % 1000, 
          DATEADD(SECOND, cast(dateModified as bigint) / 1000, '19700101'))
from sometable

See SQL Fiddle with Demo

like image 193
Taryn Avatar answered Jan 17 '23 02:01

Taryn