简体   繁体   English

SQL Server:CASE不起作用

[英]SQL Server : CASE does not work

SELECT 
   a.componentId, a.uniqueCode, 
   'sd'= CASE 
            WHEN RTRIM(LTRIM(b.name)) IS NULL OR RTRIM(LTRIM(b.uniqueCode)) IS NULL 
               THEN isnull(b.uniqueCode,'')+isnull(b.name,'') 
            WHEN RTRIM(LTRIM(b.name)) IS NULL AND RTRIM(LTRIM(b.uniqueCode)) IS NULL
               THEN isnull(b.uniqueCode,'')+isnull(b.name,'')
            ELSE b.uniqueCode + '(' + (b.name) + ')'
    END, 
    a.specialization 
FROM Doctors a 
LEFT OUTER JOIN Territories b ON a.locationId = b.componentId;

Suppose b.uniqueCode = T003 and b.name = Dhanmondi 01 , then sd should be T003(Dhanmondi 01) . 假设b.uniqueCode = T003b.name = Dhanmondi 01 ,则sd应该是T003(Dhanmondi 01)

Now if b.name = NULL then sd should be T003 , but my query result shows T003() . 现在,如果b.name = NULL则sd应该为T003 ,但我的查询结果显示为T003()

What is wrong my T-SQL query? 我的T-SQL查询出了什么问题?

Are you sure that b.name is null ? 您确定b.name为null吗? If it has an empty string instead of null you would have the result you see. 如果它有一个空字符串而不是null那么您将看到结果。 BTW, the rtrim/ltrim stuff is totally unnecessary when checking with is null and your second when will never happen because you will always end up in the first when if either column is null . 顺便说一句,在rtrim/ltrim东西是检查时完全没有必要is null和你的第二个when将永远不会发生,因为你将永远结束在第一when如果任一栏是null

This will treat empty strings as null: 这会将空字符串视为null:

SELECT a.componentId, a.uniqueCode, 'sd'=
        CASE 
            WHEN nullif(b.name, '') IS NULL OR nullif(b.uniqueCode, '') IS NULL 
            THEN isnull(b.uniqueCode,'')+isnull(b.name,'') 
            ELSE b.uniqueCode + '(' + (b.name) + ')'
        END , a.specialization 
FROM Doctors a LEFT OUTER JOIN Territories b ON a.locationId = b.componentId;

Let's sort some basics out first... 让我们先整理一些基础知识...

Your second WHEN is impossible to reach, since the earlier WHEN will always be true (either null) before the second WHEN (both null). 您无法到达第二个WHEN ,因为较早的WHEN在第二个WHEN (均为null)之前始终为true (两个null)。

RTRIM() and LTRIM() will only return NULL if the argument is NULL , so these two condition expressions are identical: RTRIM()LTRIM()将只返回NULL如果参数为NULL ,所以这两个条件表达式是相同的:

  • RTRIM(LTRIM(b.name)) IS NULL
  • b.name IS NULL

Removing redundant code, including the unreachable WHEN , will let you simplify your code considerably: 删除冗余代码,包括无法访问的WHEN ,将使您大大简化代码:

CASE
    WHEN b.name IS NULL OR b.uniqueCode IS NULL 
        THEN isnull(b.uniqueCode,'') + isnull(b.name,'')
    ELSE b.uniqueCode + '(' + b.name + ')'
END

Now that we can read it... 现在我们可以阅读...

The most likely explanation is that b.name is blank , not NULL . 最可能的解释是b.name ,而不是NULL

In SQL and all of the popular SQL products - except Oracle - a NULL and an empty string '' are two different things. 在SQL和所有流行的SQL产品中(除了Oracle), NULL和空字符串''是两回事。 So, you should be checking both options: 因此,您应该检查两个选项:

SELECT a.componentId, a.uniqueCode, 
    CASE 
        WHEN b.name IS NULL OR b.name = ''
            THEN COALESCE(b.uniqueCode, '')
        WHEN b.uniqueCode IS NULL OR b.uniqueCode = ''
            THEN b.name
            ELSE b.uniqueCode + '(' + b.name + ')'
    END AS sd
     , a.specialization 
...

or to remove leading and trailing spaces: 或删除前导和尾随空格:

    CASE 
        WHEN b.name IS NULL OR RTRIM(LTRIM(b.name)) = ''
            THEN COALESCE(RTRIM(LTRIM(b.uniqueCode)), '')
        WHEN b.uniqueCode IS NULL OR RTRIM(LTRIM(b.uniqueCode)) = ''
            THEN RTRIM(LTRIM(b.name))
            ELSE RTRIM(LTRIM(b.uniqueCode)) + '(' 
               + RTRIM(LTRIM(b.name)) + ')'
    END AS sd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM