Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert unix epoch timestamp to TSQL datetime

I have found only one similar question but for MySQL.

I was working on a web service and had to query the database (MS SQL server). Since I couldn't get the right result I decided to test the query via a SQL client. The web service uses Hibernate to access the DB and all time values are always represented as long values (unix epoch time). In order to test it, I needed to convert the unix timestamp to TSQL timestamp. This is what I came up with:

select dateadd(ms,123,'1970-01-01 00:00:00.0');

which outputs:

1970-01-01 00:00:00.123

But, my actual data was a bit bigger

select dateadd(ms,1359016610667 ,'1970-01-01 00:00:00.0');

which outputs:

Error code 0, SQL state 22001: Data truncation
Error code 8115, SQL state 22003: Arithmetic overflow error converting expression to data type int.

So, I tried:

select dateadd(ms,CAST (1359016610667 AS BIGINT) ,'1970-01-01 00:00:00.0');

which outputs the exact same error. Just to be safe I tried:

select CAST (1359016610667 AS BIGINT) 

which outputs:

1359016610667

I made sure that java long is equivalent to TSQL bigint - they are both 8 B long. Rereading the dateadd() documentation revealed the following:

DATEADD (datepart , number , date )
....
number
Is an expression that can be resolved to an int that is added to a datepart of date. User-defined variables are valid.

If I understand this correctly, it means that this approach can not be used to convert a unix timestamp to TSQL timestamp, which is, well, pardon my language, but just plain stupid.

My questions are:

  • is my interpretation of this situation correct?
  • is there any other one-liner to do this conversion in TSQL ?

PS
modifying the date argument ('1970-01-01 00:00:00.0') is not acceptable as solution. I'm debugging and I don't want to recalculate the miliseconds along :)

like image 528
linski Avatar asked Jan 24 '13 17:01

linski


People also ask

How do I convert UNIX time to normal time in SQL?

Here, we use Oracle's TO_DATE() function to construct a date of 1970-01-01. We then add our Unix timestamp to that date to get our result. In this case, we use NUMTODSINTERVAL() to convert the Unix timestamp into an interval value. The result is a DATE value.

How do I convert a timestamp to a date in SQL?

CAST() function performs the same way as CONVERT(), i.e. it too converts the value of any data type in the desired data type. Thus, we can make use of this function to convert the retrieved current timestamp in the date and time values.

How do I convert epoch to date?

Because our Epoch time is specified in milliseconds, we may convert it to seconds. To convert milliseconds to seconds, first, divide the millisecond count by 1000. Later, we use DATEADD() to add the number of seconds since the epoch, which is January 1, 1970 and cast the result to retrieve the date since the epoch.

Is Unix timestamp same as epoch?

Unix time is the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970, excluding leap seconds. This time is named the Unix epoch, because it is the start of the Unix time.


1 Answers

Easy, first add whole days, then add the remaining ms. There are 86,400,000 milliseconds in a day.

declare @unixTS bigint
set @unixTS = 1359016610667


select dateadd(ms, @unixTS%(3600*24*1000), 
    dateadd(day, @unixTS/(3600*24*1000), '1970-01-01 00:00:00.0')
)

The result is 2013-01-24 08:36:50.667

like image 51
Ben Avatar answered Oct 08 '22 04:10

Ben