这是没有 CONVERT 或 CASE 的查询:
SELECT *
FROM HastaKurumlari
WHERE '21-05-2009' between startDate and IsNull(endDate,getdate())
为了确保 Sql Server 不会为每一行评估 getdate(),您可以缓存它,尽管我很确定 Sql Server 默认情况下足够智能:
declare @now datetime
set @now = getdate()
SELECT *
FROM HastaKurumlari
WHERE '21-05-2009' between startDate and IsNull(endDate,@now)
发布查询计划可以帮助解释查询缓慢的原因:
SET SHOWPLAN_TEXT ON
go
SELECT *
FROM HastaKurumlari
WHERE CONVERT(SMALLDATETIME,'21-05-2009',103)
BETWEEN startDate
AND (CASE WHEN endDate IS NULL THEN GETDATE() ELSE endDate END)