Converting of the MySQL date stamp (milliseconds) to SQL server millisecond

394 views Asked by At

I have a date in MySQL: 1395774998. I understand this date is a number of milliseconds.

I used epochconverter.com to convert this date to:

**GMT: Tue, 25 Mar 2014 19:16:38 GMT**

I would like to convert this MySQL date to SQL Server without losing information.

1

There are 1 answers

1
Roman Marusyk On

Please try this

DECLARE @Datetime BIGINT, @Result datetime, @LocalDatetime  BIGINT;

SET @Datetime = 1395774998
SET @LocalDatetime = @Datetime - DATEDIFF(second,GETDATE(),GETUTCDATE())
SET @Result = (SELECT DATEADD(second,@LocalDatetime , CAST('1970-01-01 00:00:00' AS datetime)))

select @Result

OR function

CREATE FUNCTION dbo.ufn_ConvertUnixTimestampToDateTime (@Datetime BIGINT)
RETURNS DATETIME
AS
BEGIN
    DECLARE @LocalDatetime BIGINT
    SET @LocalDatetime = @Datetime - DATEDIFF(second,GETDATE(),GETUTCDATE())
    RETURN (SELECT DATEADD(second,@LocalDatetime, CAST('1970-01-01 00:00:00' AS datetime)))
END;