将数字转换为日期时间

问题描述

我正在查询包含此列的 sql Server 表:

dateCreated(numeric(19,0),null)

看来这是 UNIX 时代。我想从表中选择所有内容并将 dateCreated 列转换为适合报告摘录的内容。我已经通过我发现的其他一些帖子尝试了以下内容

How to convert Epoch time to date?

SELECT 
    DATEADD(S,t.dateCreated,CAST('1970-01-01' as datetime)),t.user,t.machine  
FROM
    table t

此的任何变体都会产生错误

将表达式转换为 int 数据类型时出现算术溢出错误

如何将此列转换为 datetime 格式?

解决方法

发生这种情况是因为对于 t 中的至少一行,const winningIndices = [ [0,1,2],[6,7,8],[0,3,6],[2,5,4,6] ]; const x = [0,2,5]; const result = winningIndices .map((item) => { const xIncludesItem = item .map((i) => x.includes(i)) .every(includes => includes); return { item,xIncludesItem }; }) .filter(result => result.xIncludesItem); console.log(result); 大于 2147483647。

2147483647 是 SQL Server 中 int 数据类型的最大值。

2147483647 是 EPOCH 时间的 2038-01-19 03:14:07。

如果 t.dateCreated 确实是 EPOCH 时间并且 t.dateCreated 的值高于 2038-01-19 03:14:07,则该查询将不起作用。

也许你应该检查一下:

  • 如果 t.dateCreated 真的是 EPOCH 时间。
  • t.dateCreated > 2147483647 的行,以防出现异常值。

要检查的另一件事是,EPOCH 时间是否以毫秒表示。

在这种情况下,查询应该是

t.dateCreated

但是你会错过几毫秒。