Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Converting Unix time to SQL Server datetime

From Wiki

Jump to: navigation, search

Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds elapsed since midnight UTC of January 1, 1970, not counting leap seconds

Here is how you do it in SQL Server. To get the datetime value for a value stored in Unix time you need to add the seconds to the base Unit time which is '19700101 05:00:00:000'

The following code will give you an output of '2010-05-25 07:56:23.000'

  1. SELECT dateadd(s,1274756183,'19700101 05:00:00:000')

To get the datetime for Unix time for January 1, 2001 run this

  1. SELECT dateadd(s,946666800,'19700101 05:00:00:000')

See also http://wiki.lessthandot.com/index.php/Epoch_Date for the reverse, converting from datetime to Unix time

Contributed by: --SQLDenis 17:32, 25 May 2010 (GMT)

Part of SQL Server Programming Hacks

Section Dates

775 Rating: 2.4/5 (77 votes cast)