SQL Datediff溢出 - 空溢出

时间:2017-02-01 12:06:13

标签: sql sql-server datediff integer-overflow

我正在运行一个日期,以便在两个时间戳之间返回微秒差异,所以我相信在溢出前可以处理的最大差距大约是33分钟。

它们超出了我的分析范围。我期待一些功能可以忽略任何溢出,因此我遇到了:

set arithabort off
set ansi_warnings off
set arithignore on

我的理解是这些应该禁止警告并防止中止查询,返回NULL,否则会发生溢出。

但是,实际上在查询中使用这些仍会导致:

The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.

我有点困惑。日期不是溢出而不是算术溢出?我应该使用不同的“设置”提示吗?

1 个答案:

答案 0 :(得分:1)

我不确定溢出,但您可以使用case

select (case when datediff(second, t1, t2) > 30
             then cast(datediff(second, t1, t2) as bigint) * 1000000
             else cast(datediff(microsecond, t1, t2) as bigint)
        end)