SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert Bigint time to datetime


Convert Bigint time to datetime

Author
Message
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41278 Visits: 19501
PSB (4/21/2014)
I have a couple of 14 digit int time in my table

SELECT DATEADD(hh,-5,dateadd(s, convert(bigint, 61353491400000) / 1000, convert(datetime, '1-1-1970 00:00:00')))

The above query throws an error :

Arithmetic overflow error converting expression to data type int.

Please advise how di I resolve it.

Thanks,
PSB


Look at the numbers you are working with,
INT Max Value => (2^31 - 1) = 2147483647
61353491400000 / 1000 = 61353491400

almost 30 times the maximum integer value, suggest you change your code to
SELECT DATEADD(hh,-5,dateadd(s, convert(bigint, 61353491400000) / 1000000, convert(datetime, '1-1-1970 00:00:00')))


and work from that.
Cool
PSB
PSB
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2559 Visits: 1565
According to the query time returned is 1970-03-12 19:15:49.000 .

SELECT DATEADD(hh,-5,dateadd(s, convert(bigint, 6135349140000) / 1000000, convert(datetime, '1-1-1970 00:00:00')))

The correct time that should be returned is 2014 -03-20 03:27:00.00
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96494 Visits: 38983
PSB (4/21/2014)
According to the query time returned is 1970-03-12 19:15:49.000 .

SELECT DATEADD(hh,-5,dateadd(s, convert(bigint, 6135349140000) / 1000000, convert(datetime, '1-1-1970 00:00:00')))

The correct time that should be returned is 2014 -03-20 03:27:00.00


That is interesting, especially since earlier you said the 1397750400000 was equivalent to 2014-04-17 11:00:00.00. Interesting that a larger value should represent an earlier time.

I guess we need more information if we are going to help you figure this out. Does the application that reads this data output the date/time in a readable format? Perhaps if you show us a couple of rows of data as displayed in the application along with the corresponding raw data we could help figure this out.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41278 Visits: 19501
PSB (4/21/2014)
According to the query time returned is 1970-03-12 19:15:49.000 .

SELECT DATEADD(hh,-5,dateadd(s, convert(bigint, 6135349140000) / 1000000, convert(datetime, '1-1-1970 00:00:00')))

The correct time that should be returned is 2014 -03-20 03:27:00.00


Question: where does the reference date of 1-1-1970 come from, are you working with unix dates:

Secondly, consider these numbers for an average year (365.25 days)
8766 Hours
525960 Minutes
31557600 Seconds
Cool
Federico Iori
Federico Iori
Old Hand
Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)

Group: General Forum Members
Points: 327 Visits: 300
Lynn Pettis (4/17/2014)
PSB (4/17/2014)

select DATEADD(Minute, 1397750400000, '1970-00-01')
Getting an overflow error .


Arithmetic overflow error converting expression to data type int.


Yep, told you that would happen earlier.

Now, what date and time does 1397750400000 represent??


Give a look to http://www.sqlservercentral.com/scripts/TIMESTAMP/76877/
By the way, I believe they are seconds, not minutes
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14781 Visits: 11848
If it turns out that the time is stored in milliseconds with a base of 1970-01-01 00:00:00.000, then this code will work.


-- Milliseconds since 1970-01-01 00:00:00.000
declare @TimeMS bigint = 1397750400000;

select
[Date/Time] =
-- Verify time can be converted to datetime
case when @TimeMS between -6847804800001 and 253402300799998
then dateadd(ms,@TimeMS%86400000,(@TimeMS/86400000)+25567)
else null end



Results:

Date/Time
-----------------------
2014-04-17 16:00:00.000



This will do the reverse conversion:

declare @DateTime datetime = '2014-04-17 16:00:00.000'

select
TimeMS =
(datediff(dd,25567,@DateTime)*00000086400000)+
datediff(ms,dateadd(dd,datediff(dd,0,@DateTime),0),@DateTime)



Results:

TimeMS
-------------------
1397750400000



Note:
I just realized this is an old post, but the answer is correct if anyone cares.
It's just a bit of code I had laying around from an old project. Cool
PSB
PSB
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2559 Visits: 1565
Hi,

Some of the values are also 14 digit . If I use your code below , it returns incorrect dates as shown below.

declare @TimeMS bigint = 61353491400000;

select
[Date/Time] =
-- Verify time can be converted to datetime
case when @TimeMS between -6847804800001 and 253402300799998
then dateadd(ms,@TimeMS%86400000,(@TimeMS/86400000)+25567)
else null end

--INCORRECT RESULT

Date/Time
3914-03-20 20:30:00.000
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search