Converting UnixTime To DateTime

Unixtime is 32-bit integer. It’s a second counter. A problem is converting datetime to unix time on mssql server. This solution convert unixtime to datetime.

For a more information for Unix Time by wikipedia.

Declare @UnixTime int
Set @UnixTime = 1219366020 -- 08.22.2008 00:47:00
Select dateadd(ss,@UnixTime,'1970-01-01')

To Function:

Create Function fn$UnixTimeToDatetime(@UnixTime int)
Returns Datetime
Begin
Return dateadd(ss,@UnixTime,'1970-01-01')
End

Go


Usage:

Select dbo.fn$UnixTimeToDatetime(1219366020) As DateAndTime


Leave a comment