Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

TSQL fast unix timestamp conversion

By Federico Iori,

This is a fast way to convert a bigint value ( int is not big enough) representing a Unix UTC timestamp to datetime. It is a quite common problem . This solution is very fast and suitable for processing large number of rows, because uses pure TSQL and no UDF .

It is necessary to create a dimension DIM_DAY, containing also the information if a given day is summer or winter time .

Then, there is a clear example of usage .

Note that Offset is 2009 because from 1970 would overflow datatype int and dateadd accepts only int.

Slovakia has GMT+1 , in other time zone just change the values 7200 and 3600 in the select .

This method is fast, since uses TSQL hash join , there is only small disavatage that changing form summer to winter time is assumed to be at 00:00 , while in reality is at 02:00 am, but ofr business purpose in general it makes small difference.

If the timestamp is espressed in Hex format, you can convert it easilyt into numeric as described in

Total article views: 471 | Views in the last 30 days: 1
summer time