IIF

Version >= SQL Server 2012

返回两个值中的一个,具体取决于给定的布尔表达式是否为 true 或 false。

参数:

  1. boolean_expression 评估确定要返回的值
  2. 如果 boolean_expression 评估为真,true_value 返回
  3. 如果 boolean_expression 评估为假,则 false_value 返回
SELECT IIF (42 > 23, 'I knew that!', 'That is not true.') AS iif_result

iif_resultI knew that!

Version < SQL Server 2012

IIF 可能会被 CASE 声明取代。上面的例子我写的是

SELECT CASE WHEN 42 > 23 THEN 'I knew that!' ELSE 'That is not true.' END AS iif_result

iif_resultI knew that!