Unix Date Format to SQL DateTime

So if you come across Unix time format (https://en.wikipedia.org/wiki/Unix_time) and you want to make out sens or use it in your MS sql database or basic query writing in Ms SQL  following script would help you out:

 

select cast(19096 as dateTime)+cast(cast(19096 / 86400.0 as datetime)+cast(‘1900-01-01 00:00:00.000’ as datetime) as DateTime)-2

 

 

19096 being the unix time

One  thing that you might have not noticed is that i subtracted 2 from the result, why did i do that ?
Answer : Removal of precision error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s