SQL 存储过程中日期参数的通配符

SQL wildcard for date parameter in stored procedure

我有一个只有一个参数的存储过程@date

该过程是一个 select 语句,其中包含:

where dateTimeGmt between @date and dateadd(dd,9,@date)

我希望能够调用此过程并使用 table.

中的所有日期获取所有结果

@date 设置为 %NULL%-%-% 无效。

如果 exec 没有传递参数,是否有一种优雅的方法让过程忽略 where 子句?

I'd like to be able to call this procedure and get all the results using all the dates within the table.

Setting @date to % or NULL or %-%-% doesn't work.

% 比较仅适用于字符串类型,不适用于日期

Is there an elegant way of having the procedure ignore that where clause if no parameter is passed with the exec?

尝试如下更改您的 where 子句。

where (dateTimeGmt between @date and dateadd(dd,9,@date) ) or @date  IS NULL