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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...