ISNULL - ON 子句

ISNULL - ON clause

我正在尝试在此 ON 子句上使用 ISNULLCOALESCECASE,但出现错误。

'Incorrect syntax near the keyword 'between'.

我需要在另一个 table 的两个日期之间检查一个日期,但如果第二个 table 上不存在该日期,则使用前一个日期。

talbe1 t1 JOIN table2 t2
ON t1.code = t2.code 
AND ISNULL(cast(dateadd(d,-1,t1.UtcFinishTime) as date) BETWEEN t2.TransactionFirstDate and t2.TransactionLastDate,cast(dateadd(d,-2,t1.UtcFinishTime) as date) BETWEEN t2.TransactionFirstDate and t2.TransactionLastDate) 

非常感谢。

无法检查,但不在我的脑海中,下面的查询可能会有所帮助-

SELECT * 
from table1 t1 JOIN table2 t2
     ON t1.code = t2.code AND 
     CASE 
       WHEN dateadd(day,-1,t1.UtcFinishTime) is null 
       THEN cast(dateadd(day,-2,t1.UtcFinishTime) as date) 
       END 
     BETWEEN t2.TransactionFirstDate and t2.TransactionLastDate

您的语法不正确,因为 ISNULL 需要成为语句的一部分,因为它 returns 是值;而不是整个声明,所以你可能会找到类似的东西:

AND ISNULL(
           cast(dateadd(d,-1,t1.UtcFinishTime) as date), 
           cast(dateadd(d,-2,t1.UtcFinishTime) as date)
          ) 
     BETWEEN t2.TransactionFirstDate and t2.TransactionLastDate

或使用 CASE 功能。

但为了性能起见,我建议您事先进行检查,然后针对任何一种情况进行具体查询。