不适用于雪花 - "IFF" 声明

not working in snowflake - "IFF" statement

我正在尝试在 Snowflake 视图中实现以下语句

IFF("Closed" = TRUE,DATEDIFF(DAY,TO_DATE("ActualFinishDate"),current_date()),FALSE) - 得到 True/False

它不会工作 如果我给 IFF("Closed" = TRUE,'1234',FALSE) - 这有效,我根据 closed

的值得到 1234 或 False

有人可以帮助我为什么 DATEDIFF(DAY,TO_DATE("ActualFinishDate"),current_date()) 在 IFF 中没有给出结果 如果我单独执行它,我测试了语句是否正确和正确的值。

一切都是关于数据类型

CASE

In the second form of CASE, each value is a potential match for expr. The value can be a literal or an expression. The value must be the same data type as the expr, or must be a data type that can be cast to the data type of the expr.

区别如下:

DATEDIFF returns INTEGER, BOOLEAN
vs
'1234'   string literal,  BOOLEAN

对于 Booelan,它的工作原理如下:

TO_BOOLEAN:

For a text expression, string must be:

        'true', 't', 'yes', 'y', 'on', '1' return TRUE.

        'false', 'f', 'no', 'n', 'off', '0' return FALSE.

        All other strings return an error.

Strings are case-insensitive.

For a numeric expression:

        0 returns FALSE.

        All non-zero values return TRUE.

编辑:

为了 return 可以使用字符串而不是布尔显式转换:

IFF("Closed" = TRUE,TO_VARCHAR(DATEDIFF(DAY,TO_DATE("ActualFinishDate"),current_date())),FALSE)

-- "Closed" column is probably boolean so there is no need to ` = TRUE`:
IFF("Closed",TO_VARCHAR(DATEDIFF(DAY,TO_DATE("ActualFinishDate"),current_date())),FALSE)