SQL:将 DATEADD 与 bigint 结合使用 [英] SQL: Using DATEADD with bigints

查看:49
本文介绍了SQL:将 DATEADD 与 bigint 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 SQL 可以将 javascript 日期转换为 SQL 日期,效果很好.但是,我遇到了一些过大并导致异常的数据:

I have some SQL to convert javascript dates to SQL dates which works great. However, I've encoutered some data which is too large and is causing an exception:

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

Arithmetic overflow error converting expression to data type int

这是有问题的 SQL:

Here is the SQL in question:

  DATEADD(MILLISECOND, cast(569337307200000 as bigint) % 1000, DATEADD(SECOND, cast(569337307200000 as bigint) / 1000, '19700101'))

我在 SQL Server 2008 上运行它.

I am running this on SQL Server 2008.

推荐答案

根据 MSDN,在DATEADD(datepart , number , date )

number 是一个表达式,可以解析为一个 int 并添加到日期的一个日期部分.用户定义的变量有效.如果你用小数指定一个值,小数会被截断并不是四舍五入.

number is an expression that can be resolved to an int that is added to a datepart of date. User-defined variables are valid. If you specify a value with a decimal fraction, the fraction is truncated and not rounded.

另请注意,即使您将 number 指定为整数,也取决于您的日期 &datepart,它可能会溢出sql server 2008的最大日期范围,即31-12-9999

Also notice that even if you give number as an integer, depending on your date & datepart, it could overflow the max range of the date which is 31-12-9999 for sql server 2008

数字必须是整数.这是一个测试演示

Number has to be an integer. Here is a Test Demo

这篇关于SQL:将 DATEADD 与 bigint 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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