Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

TSQL fast unix timestamp conversion

By Federico Iori, 2011/12/02

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 http://www.sqlservercentral.com/Contributions/76413

Total article views: 419 | Views in the last 30 days: 4
 
Tags
converter    
datetime    
summer time    
timestamp    
t-sql    
unix    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones