11

I have a 'datetime' column with value 2013-03-22 15:19:02.000

I need to convert this value into epoch time and store it in a 'bigint' field

The actual epoch value for the above time is, 1363945741898, when I use

  select DATEDIFF(s, '1970-01-01 00:00:00', '2013-03-22 15:19:02.000')

I get, 1363965542, when I use

select DATEDIFF(ms, '1970-01-01 00:00:00', '2013-03-22 15:19:02.000')

I get,

Msg 535, Level 16, State 0, Line 1 The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.

How to get the exact epoch value from the 'datetime' field

I use SQL Server 2008. Also this should work with 2005.

8
  • What timezone is your datetime in? Looks some hours and a half hour away from GMT. Maybe in India? (I'm wondering if SQL Server is maybe using GMT for the timezone for 1970-01-01 as it doesn't have timezone information going back that far, and your local timezone for 2013.) Commented Mar 26, 2013 at 10:37
  • 2
    You say you want it in epoch value = unix time, and that is in seconds, so why do you query the difference in milliseconds in the second query?
    – aweis
    Commented Mar 26, 2013 at 10:38
  • @MattGibson India IST
    – itsraja
    Commented Mar 26, 2013 at 10:40
  • Hrm. Out of curiosity, what does select DATEDIFF(s, '1970-01-01 00:00:00Z', '2013-03-22 15:19:02Z') give you? (Using the Z should result int a datetimeoffset based on GMT rather than a datetime.) Commented Mar 26, 2013 at 10:45
  • 1
    @aweis I should store it in milli sec
    – itsraja
    Commented Mar 26, 2013 at 10:45

4 Answers 4

12

Here is an example, not tested, written from free hand :)

declare @v_Date datetime
set @v_Date = '2013-03-22 15:19:02.000'

declare @v_DiffInSeconds integer
declare @v_DiffInMSeconds bigint

select @v_DiffInSeconds = DATEDIFF(s, '1970-01-01 00:00:00', @v_Date)
select @v_DiffInMSeconds = cast(@v_DiffInSeconds as bigint) * 1000 + cast(DATEPART(ms, @v_Date) as bigint)

Edit I have made this example below to illustrate the time zone conversion. The given time stamp (in seconds where I have removed the last three digits "898") is here converted to the local IST time zone by adding the 5.5 hours (19800 seconds) and I convert it back to the time stamp from local time to GMT again. Below calculations matches the values in the question (in seconds).

declare @v_time datetime
set @v_time = '1970-01-01 00:00:00'

declare @v_date datetime
set @v_date = '2013-03-22 15:19:01'

-- This returns "March, 22 2013 15:19:01"
select dateadd(s, (1363945741 + 19800), @v_time)

-- This returns "1363945741"
select datediff(s, @v_time, @v_date) - 19800
5
  • This also returns 1363965542000
    – itsraja
    Commented Mar 26, 2013 at 12:37
  • I tried converting the integer values on epochconverter.com and 1363945741898 is evaluated to Fri, 22 Mar 2013 09:49:01 GMT so im not sure how you have calculated you value since you get it to 2013-03-22 15:19:02.000
    – aweis
    Commented Mar 26, 2013 at 16:24
  • in my timezone it is, Friday, March 22, 2013 3:19:01 PM GMT+5.5
    – itsraja
    Commented Mar 27, 2013 at 5:30
  • I have added an example to the answer where I convert from and back between GMT and IST.
    – aweis
    Commented Mar 27, 2013 at 9:28
  • In your first code block I subtracted 19800 from seconds before calculating ms. It gave me correct result. Thanks.
    – itsraja
    Commented Apr 14, 2013 at 10:20
4

When tried to get exact milliseconds we get the overflow exception. we can get the values till seconds and multiply with 1000.

This is equivalent to new Date().getTime() in javascript:

Use the below statement to get the time in seconds.

SELECT cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint)

Use the below statement to get the time in milliseconds.

SELECT cast(DATEDIFF(s, '1970-01-01 00:00:00.000', '2016-12-09 16:22:17.897' ) as bigint) * 1000

convert epoch to human readable date time using below statement:

select DATEADD(s, 1481300537, '1970-01-01 00:00:00')
1
  • 1
    last statement results in: Arithmetic overflow error converting expression to data type int. if you have a larger date.
    – FatAlbert
    Commented Dec 19, 2017 at 10:17
1

Epoch to datetime

create function [dbo].[EpochToDate](@Date bigint)
returns datetime
begin
    return (select dateadd(s, @Date, '19700101'))
end 
0

Use below code to get human readable date from epoch time

select DATEADD(s,convert(bigint,@date)/2, DATEADD(s, convert(bigint,@date)/2, '1970-01-01 00:00:00'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.