Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 仅当年份不是9999时才使用DATEADD()_Sql Server - Fatal编程技术网

Sql server 仅当年份不是9999时才使用DATEADD()

Sql server 仅当年份不是9999时才使用DATEADD(),sql-server,Sql Server,我正在尝试执行以下SQL查询: SELECT * FROM MyTable WHERE GETDATE() AS CurrentDateTime < DATEADD(day, 1, EndDateTime) 选择* 从MyTable 其中GETDATE()作为CurrentDateTime

我正在尝试执行以下SQL查询:

SELECT * 
FROM MyTable
WHERE GETDATE() AS CurrentDateTime < DATEADD(day, 1, EndDateTime)
选择*
从MyTable
其中GETDATE()作为CurrentDateTime
问题是某些EndDateTime的日期可能最高,当我尝试运行此查询时,我得到一个错误:
向“datetime”列添加值导致溢出。

如果日期为9999年,你知道如何使用EndDateTime本身而不是EndDateTime+1天吗

SELECT *
FROM MyTable 
WHERE GETDATE() AS CurrentDateTime < case 
      when year(EndDateTime) <> 9999 
      then DATEADD(day, 1, EndDateTime) 
      else EndDateTime 
end
选择*
从MyTable
其中GETDATE()作为CurrentDateTime<大小写
年份(EndDateTime)9999时
然后添加日期(第1天,EndDateTime)
else EndDateTime
结束

按其他顺序执行逻辑:

SELECT * 
FROM MyTable
WHERE EndDateTime > DATEADD(day, -1, GETDATE() );

这也更好,因为查询可以利用第一个键为
EndDateTime

的索引,我认为您需要使用与
datetime
不同的列类型。请问您为什么要使用9999年的代码?你是否在等待安然股票再次变得有价值?