一個簡單的解決辦法是:如果日期爲之間或等於兩個間隔日期
運營商之間
Select [Id], [Name] FROM [Product]
where
@Date Between CreatedAt AND isnull(UpdatedAt,@date)
的返回true,所以如果CreatedAt> @date返回假,則即使當UpdatedAt爲空而這兩者之間的上限將是@Date。
測試例如:
declare @date date = cast('20100601' as date)
;with Product as
(
select [Id], [Name] ,[CreatedAt],[UpdatedAt]
from
(values
(1,'Product 1',cast('20100101' as date),cast('20110101' as date))
,(2,'Product 2',cast('20100101' as date),null)
,(3,'Product 3',cast('20100701' as date),null)
) a([Id], [Name] ,[CreatedAt],[UpdatedAt])
)
Select
[Id], [Name] FROM [Product]
where
@Date Between CreatedAt AND isnull(UpdatedAt,@date)
其中@date CreatedAt和ISNULL(UpdatedAt,'12 /9999分之31' )..或任何日期遠在你想要的未來之間。 – Jeremy