Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Convert Bigint time to datetime Expand / Collapse
Author
Message
Posted Monday, April 21, 2014 6:10 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:44 PM
Points: 1,913, Visits: 5,269
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.
Post #1563447
Posted Monday, April 21, 2014 6:47 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 5:21 AM
Points: 296, Visits: 1,058
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
Post #1563460
Posted Monday, April 21, 2014 7:05 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:53 PM
Points: 20,680, Visits: 32,278
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.



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)
Post #1563467
Posted Monday, April 21, 2014 7:25 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:44 PM
Points: 1,913, Visits: 5,269
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
Post #1563476
Posted Tuesday, July 1, 2014 7:23 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 8, 2014 7:35 AM
Points: 33, Visits: 291
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
Post #1588016
Posted Tuesday, July 1, 2014 8:04 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, September 18, 2014 12:39 AM
Points: 3,105, Visits: 11,494
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.






Post #1588047
Posted Wednesday, July 30, 2014 8:27 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 5:21 AM
Points: 296, Visits: 1,058
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
Post #1597760
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse