## Convert Bigint time to datetime

 Author Message Eirikur Eiriksson SSC Guru Group: General Forum Members Points: 94718 Visits: 20693 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. PSB SSCertifiable Group: General Forum Members Points: 6881 Visits: 1653 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 SSC Guru Group: General Forum Members Points: 225974 Visits: 40423 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. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Eirikur Eiriksson SSC Guru Group: General Forum Members Points: 94718 Visits: 20693 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 Federico Iori Right there with Babe Group: General Forum Members Points: 719 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 SSC-Dedicated Group: General Forum Members Points: 36236 Visits: 11933 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. PSB SSCertifiable Group: General Forum Members Points: 6881 Visits: 1653 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