如何在SQL Server中将bigint(UNIX时间戳)转换为datetime?
问题描述:
如何在SQL Server中将bigint(UNIX时间戳)转换为Datetime?
How can I convert bigint (UNIX timestamp) to Datetime in SQL Server?
答
try:
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