在select CASE中检查NULL

时间:2014-06-27 14:39:04

标签: sql sql-server tsql sql-server-2012

当n.ownerid为null时,它将永远不会执行此部分:

...........,case n.ownerid
    when NULL then 
    (
    select systemuserid
    from crm_systemuserbase 
    where firstname = 'CRM' and lastname='Admin' 
)...........

以下是更多周边代码:

,case n.ownerid
    when NULL then 
    (
    select systemuserid
    from crm_systemuserbase 
    where firstname = 'CRM' and lastname='Admin'
    )
    when '6e99ff04-f498-e311-93f3-005056a37b31' then
    (
    select systemuserid
    from crm_systemuserbase 
    where firstname = 'CRM' and lastname='Admin'
    )
    end as OwnerID

在SELECT CASE中如何检查字段问题的值是否为空?

2 个答案:

答案 0 :(得分:7)

is

比较时,请使用null运算符
case when n.ownerid is null then ...
     when n.ownerid = '6e99ff04-f498-e311-93f3-005056a37b31' then ...
end as OwnerID

答案 1 :(得分:2)

你也可以使用COALESCE表达式:

CASE when COALESCE(n.ownerid, '(nullVal)') = '(nullVal)' . . .