Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bigint to datetime

I want to convert a value from bigint to datetime.

For example, I'm reading the HISTORY table of teamcity server. On the field build_start_time_server, I have this value on one record 1283174502729.

How can I convert it to a datetime value?

like image 590
Chris Avatar asked Sep 06 '10 09:09

Chris


People also ask

How do you convert Bigint to Pyspark timestamp?

You can use from_unixtime/to_timestamp function in spark to convert Bigint column to timestamp .


2 Answers

Does this work for you? It returns 30-8-2010 13:21:42 at the moment on SQL Server 2005:

select dateadd(s, convert(bigint, 1283174502729) / 1000, convert(datetime, '1-1-1970 00:00:00'))

I've divided by 1000 because the dateadd function won't work with a number that large. So you do lose a little precision, but it is much simpler to use.

like image 146
Rob Avatar answered Sep 22 '22 06:09

Rob


Slightly different approach:

Your scenario:

SELECT dateadd(ms, 1283174502729 / 86400000, (1283174502729 / 86400000) + 25567)
FROM yourtable

Generic code:

SELECT dateadd(ms, yourfield / 86400000, (yourfield / 86400000) + 25567)
FROM yourtable

Output:

August, 30 2010 00:00:14

SQL Fiddle: http://sqlfiddle.com/#!3/c9eb5a/2/0

like image 26
Matt Avatar answered Sep 22 '22 06:09

Matt