我正在使用下面的查询
SELECT *
FROM [dbo].[QUANTITY]
WHERE getdate() BETWEEN fromdate AND todate如果todate是null (1900-01-01 00:00:00.000),它只会过滤掉它们。我甚至希望使用todate的字段在结果中是null (1900-01-01 00:00:00.000)。这是如何做到的呢?
发布于 2015-04-17 18:33:44
您可以使用and和or运算符的组合显式地检查它:
SELECT *
FROM [dbo].[QUANTITY]
WHERE (getdate() >= fromdate AND
(todate IS NULL OR
todate = '1900-01-01 00:00:00.000')
) OR
getdate() BETWEEN fromdate AND todate发布于 2015-04-17 18:43:59
这条线上似乎有一个.Net或某个程序保存Date.MinValue,如果应用程序无法替换,则始终可以在
datefromparts(1900,1,1)并将where子句更改为:
where getdate() > fromdate
and (todate = datefromparts(1900,1,1) or getdate() <= todate)这将占用最大或无限制的日期。如果可能的话,为了防止出现奇怪的结果,我也会用
declare @testDate datetime = getdate(); 并在前面的where子句中使用该变量,而不是getdate()。
发布于 2015-04-17 18:52:37
SELECT *
FROM [dbo].[QUANTITY]
WHERE getdate() >= fromdate
AND getdate() <= COALESCE(todate
,NULLIF(todate,'1900-01-01 00:00:00.000')
,'9999-12-31 23:59:59.997'
)https://stackoverflow.com/questions/29706804
复制相似问题