SQL Server: ISNULL与NULLIF和COALESCE

ISNULL是判断是否为NULL

而NULLIF是把值换成NULL

COALESCE是用别的来代替NULL

SELECT employee_id,first_name,last_name,NULLIF (SALES_QUOTA,-1) as Quota

FROM employees

就是把-1变成 NULL

COALESCE(表达式1,表达式2,....表达式n)

从前到后,谁不是NULL就显示谁

Select employee_id,first_name,last_name,

     COALESCE ( appt_quota,(Select Min(appt_quota) From employees),0 ) AS quota
From employees
Where department = 'Marketing'

附:
ISNULL(check_expression, replacement_value)

  • check_expression 与 replacement_value 数据类型必须一致
  • 如果 check_expression 为 NULL,则返回 replacement_value
  • 如果 check_expression 不为 NULL,则返回 check_expression

NULLIF 用于检查两个表达式,语法:
NULLIF(expression, expression)

  • 如果两个 expression 相等,则返回 NULL,该 NULL 为第一个 expression 的数据类型
  • 如果两个 expression 不相等,则返回第一个 expression

From: http://hi.baidu.com/usher_gml/blog/item/98c98c3411b44a335ab5f57f.html

原文地址:https://www.cnblogs.com/emanlee/p/1430419.html