I tried out many answers already given for converting EPOCH to SQL Server datetime. They work when the EPOCH has up to a billion seconds. But as soon as it crosses the Trillion mark it goes bust! E.g. -->
1. SELECT dateadd(MCS,1351187877744,'1970-01-01')
2. SELECT dateadd(NS,1351187877744,'1970-01-01')
3. SELECT dateadd(NANOSECOND, 1351187877744, '1970-01-01 00:00:00.0000000')
4. SELECT convert(bigint, datediff(ss, '01-01-1970 00:00:00',1351187877744))
All the above fail with the following overflow error: "Arithmetic overflow error converting expression to data type int."
Interestingly when I input this date on this site it returns the correct values.
Any suggestions how to do this in a way that works for EPOCHs which are of any magnitude (>trillion secs etc)
Try this one -
MSDN :
DATEADD: The number argument cannot exceed the range of int. In the following statements, the argument for number exceeds the range of int by 1. The following error message is returned: "Msg 8115, Level 16, State 2, Line 1. Arithmetic overflow error converting expression to data type int."
Query:
DECLARE
@Date DATETIME = '19700101'
, @MaxInt INT = 2147483647
, @ms BIGINT = 1351187877744
WHILE @ms != 0 BEGIN
SELECT @Date = DATEADD(ms, CASE WHEN @ms > @MaxInt THEN @MaxInt ELSE @ms END, @Date)
SELECT @ms = CASE WHEN @ms - @MaxInt < 0 THEN 0 ELSE @ms - @MaxInt END
END
SELECT @Date
Output:
2012-10-25 17:57:57.533