一尘不染

如何在SQL Server中将bigint(UNIX时间戳)转换为日期时间?

sql

如何在SQL Server中将UNIX时间戳(bigint)转换为DateTime?


阅读 436

收藏
2021-05-05

共1个答案

一尘不染

尝试:

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