如何在SQL Server中将bigint(UNIX时间戳)转换为datetime? [英] How can I convert bigint (UNIX timestamp) to datetime in SQL Server?

查看:553
本文介绍了如何在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

这篇关于如何在SQL Server中将bigint(UNIX时间戳)转换为datetime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆