Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Convert Bigint time to datetime Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, April 21, 2014 6:10 AM
 SSCrazy Group: General Forum Members Last Login: Today @ 11:14 AM Points: 2,420, Visits: 6,732
 PSB (4/21/2014)I have a couple of 14 digit int time in my tableSELECT 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,PSBLook at the numbers you are working with,INT Max Value => (2^31 - 1) = 214748364761353491400000 / 1000 = 61353491400almost 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
 Old Hand Group: General Forum Members Last Login: Tuesday, September 30, 2014 5:22 AM Points: 300, Visits: 1,070
 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 Group: General Forum Members Last Login: Today @ 4:56 AM Points: 20,815, Visits: 32,748
 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.00That 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.
Post #1563467
 Posted Monday, April 21, 2014 7:25 AM
 SSCrazy Group: General Forum Members Last Login: Today @ 11:14 AM Points: 2,420, Visits: 6,732
 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.00Question: 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 Hours525960 Minutes31557600 Seconds
Post #1563476
 Posted Tuesday, July 1, 2014 7:23 AM
 SSC Rookie Group: General Forum Members Last Login: Wednesday, September 24, 2014 3:19 AM Points: 33, Visits: 292
 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 Group: General Forum Members Last Login: 2 days ago @ 3:34 PM Points: 3,109, Visits: 11,516
 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.000declare @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
 Old Hand Group: General Forum Members Last Login: Tuesday, September 30, 2014 5:22 AM Points: 300, Visits: 1,070
 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 RESULTDate/Time3914-03-20 20:30:00.000
Post #1597760

 Permissions