gpt4 book ai didi

SQL不允许将日期列转换为日期时间?

转载 作者:行者123 更新时间:2023-12-02 15:09:33 26 4
gpt4 key购买 nike

Select * 
from tableA
inner join tableB on tableA.id = tableB.aid
and cast(a.date AS DATETIME) = CAST('2015-08-24' AS DATETIME)

tableA.date 中存储的值为“2015-08-24”,表示数据没有问题。

当我执行上面的语句时,我得到

The conversion of a date data type to a datetime data type resulted in an out-of-range value

我可以知道为什么不能将date列转换为datetime吗?

最佳答案

问题的根本原因是这样的:

  • 数据类型 DATE 的可接受值范围为 01-01-000112-31-9999
  • 数据类型 DATETIME 的可接受值范围为 01-01-175312-31-9999

因此,如果您碰巧有 1753 年之前的 DATE,或者空/NULL 值 - 这将超出 DATETIME 可以处理的范围。

您应该停止在 SQL Server 2008 及更高版本中使用DATETIME。请改用 DATETIME2(n)(其中 n 代表您需要的小数秒数)。

所以试试这个:

select * 
from tableA
inner join tableB on tableA.id = tableB.aid
and cast(a.date AS DATETIME2(3)) = CAST('2015-08-24' AS DATETIME2(3))

我确信这会很好地工作。

关于SQL不允许将日期列转换为日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32568170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com