如何将 SQL 服务器时间戳转换为纪元时间戳?

2021-09-10 00:00:00 tsql sql-server

我知道如何使用 DATEADD 将纪元时间戳转换为 SQL 服务器时间戳,但我对执行相反操作的函数感兴趣.

I know how to convert an epoch timestamp to a SQL server timestamp using DATEADD, but I am interested in a function that does the reverse of that.

推荐答案

您可以使用 DATEDIFF 函数来完成,如下所示:

You can do it using DATEDIFF function like below :

select DATEDIFF(s, '1970-01-01 00:00:00', '2017-11-20 23:12:02.000') as EpochTimeStamp

输出:

EpochTimeStamp
--------------
1511219522

你已经知道我们怎样才能回到原来的日期:

You know already how can we get back the original date :

SELECT DATEADD(ss, 1511219522, '19700101') as OriginalDate

OriginalDate
-----------------------
2017-11-20 23:12:02.000

相关文章