首页 > 解决方案 > MSSQL: Date is greater than specific date or is null

问题描述

This is so basic, i'm already sorry.

Where clause is: start date greater than 7/1/18 and end date is greater than 10/1/18 or is null

This where clause gets me what I need, but I'd rather use 'ISNULL' instead of OR.

WHERE l.[Start Date]  > '07/01/2018' and (l.[End Date] > '10/1/2018' or l.[End Date] is null)

Can I say:

WHERE l.[Start Date]  > '07/01/2018' and (l.[End Date] > ISNULL(l.[End Date]) or something?

标签: sql-serverwhere-clauseisnull

解决方案


如果你愿意isnull(),你可以这样做:

WHERE l.[Start Date] > '07/01/2018' and isnull(l.[End Date], '11/1/2018') > '10/1/2018'

如果l.[End Date]null那么

isnull(l.[End Date], '11/1/2018')

将返回

'11/1/2018'

和条件

isnull(l.[End Date], '11/1/2018') > '10/1/2018'

将返回true
当然,您可以使用任何日期,例如'1/1/2099'


推荐阅读