SQLServer The datediff function resulted in an overflow
2016-08-18 19:41
561 查看
开发同事有一个脚本报错,很简单,简写如下:
执行后出错!
Msg 535, Level 16, State 0, Line 1
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.
datediff 函数计算溢出!难道 datediff 计算出来的的秒数超出了范围?
果然,官方文档如下:(DATEDIFF (Transact-SQL))
如果返回值超出 int 的范围(-2,147,483,648
到 +2,147,483,647),则会返回一个错误。 对于 millisecond,startdate 与enddate 之间的最大差值为
24 天 20 小时 31 分钟 23.647 秒。 对于 second,最大差值为
68 年。
查看数据库,竟然有7行时间为: 2099-08-10 11:33:37.367 ,这样转换肯定出错了,错误如上一样。可以试试。
相差秒数为 2147482800,为 整型 int 的范围内。
也可以看看 datediff 返回的类型是什么:整型
所以,数据库存在了日期较大的值,这几行日期字段原本也是错误的数据,应该把日期改掉。还有就是语句写法也有问题,不应该这么写,应改成下面的写法更规范,否则即使有索引也不能使用!
SELECT COUNT(*) FROM Logs t1 with(nolock) WHERE DATEDIFF(s, '2016-08-18 17:06:35', t1.insertTime) < 0
执行后出错!
Msg 535, Level 16, State 0, Line 1
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.
datediff 函数计算溢出!难道 datediff 计算出来的的秒数超出了范围?
果然,官方文档如下:(DATEDIFF (Transact-SQL))
如果返回值超出 int 的范围(-2,147,483,648
到 +2,147,483,647),则会返回一个错误。 对于 millisecond,startdate 与enddate 之间的最大差值为
24 天 20 小时 31 分钟 23.647 秒。 对于 second,最大差值为
68 年。
查看数据库,竟然有7行时间为: 2099-08-10 11:33:37.367 ,这样转换肯定出错了,错误如上一样。可以试试。
select DATEDIFF(s, '2000-01-01 00:00:00', '2068-01-19 04:00:00') select DATEDIFF(s, '2000-01-01 00:00:00', '2068-01-19 03:00:00') --2147482800
相差秒数为 2147482800,为 整型 int 的范围内。
也可以看看 datediff 返回的类型是什么:整型
SELECT SQL_VARIANT_PROPERTY(DATEDIFF(s, '2000-01-01 00:00:00', '2068-01-19 03:00:00'),'BaseType')
所以,数据库存在了日期较大的值,这几行日期字段原本也是错误的数据,应该把日期改掉。还有就是语句写法也有问题,不应该这么写,应改成下面的写法更规范,否则即使有索引也不能使用!
SELECT COUNT(*) FROM Logs t1 with(nolock) WHERE t1.insertTime < '2016-08-18 17:06:35'
相关文章推荐
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
- 创建存储过程错误(已解决):Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server ver
- ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
- SQL Error: 1064, SQLState: 42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
- *You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for
- 错误信息:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server ve
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server versi
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- MySql 执行语句错误 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near