在 DATEADD 间隔中使用 case 表达式

Using case expression in DATEADD interval

是否可以在 DATEADD 区间参数中使用 case 表达式?

select DATEADD(case c1 when 1 then HOUR when 2 then DAY end, c2, date) from T

Update1: 对不起,我想在where子句中使用它

select * from T where DATEADD(case c1 when 1 then HOUR when 2 then DAY end, c2, date) < GETDATE()

也许还有其他选择。

提前致谢,

不行,不能参数化DATEADDdatepart参数:

The following table lists all valid datepart arguments. User-defined variable equivalents are not valid.

您必须使用两个不同的 DATEADD 表达式,或者更改您的逻辑:

select DATEADD(hour, c2 * case c1 when 1 then 1 when 2 then 24 end, date) from T

试试下面..

select * from T 
where case c1 when 1 then DATEADD(HOUR, c2, date) 
when 2 then DATEADD(DAY, c2, date) 
end < Getdate()

不用CASE也可以查询

select * from T 
where c1=1 AND DATEADD(hour, c2, date) < getdate()
or c1=2 AND DATEADD(day, c2, date) < getdate()