将 EPOCH 时间转换为可用日期,以便与 SQL 服务器中的当前日期时间进行比较

Converting EPOCH time as a usable date for comparing to current datetime in SQL Server

数据值示例,列[last_run]值1647644503000000000

SELECT
    CAST(DATEADD(s, (last_run / 1000000000) - 18000, '19700101') AS DATETIME2) AS 'Last_Backup'

这会将其正确转换为 Last_Backup = 2022-03-18 18:01:43.0000000

当我尝试添加此查询时

, DATEDIFF(DAY, 'Last_Backup', GETDATE()) AS 'DateDifference'

我收到以下错误:

Conversion failed when converting date and/or time from character string.

如何避免此错误?

虽然不是最有效的,但您始终可以像这样重复计算:

SELECT
    CAST(DATEADD(s, (last_run / 1000000000) - 18000, '19700101') AS DATETIME2) AS 'Last_Backup',
    DATEDIFF(DAY, CAST(DATEADD(s, (last_run / 1000000000) - 18000, '19700101') AS DATETIME2), GETDATE()) AS 'DateDifference'