如果列为空或空,则SQL Server存储过程替换空字符串

时间:2015-07-22 18:21:07

标签: sql sql-server tsql

我需要检查我的存储过程中的某个coloumn是空还是null。

这是我现在所拥有的片段:

SELECT * , 
CASE WHEN a.USER IS NULL
    THEN b.ROLE
    ELSE ISNULL(a.FirstName,'') + ' ' + (ISNULL(a.MiddleName+' ','') + ISNULL(a.LastName,'')
END AS 'CustomerName'

我正在检查a.MiddleName是否为NULL但是我如何检查它是否为空,如果它是空的只是插入一个空字符串(没有空格)?

由于

2 个答案:

答案 0 :(得分:2)

更改为

SELECT 
    * , 
    CASE 
       WHEN a.USER IS NULL
         THEN b.ROLE
         ELSE CASE 
                WHEN ISNULL(a.MiddleName, '') = '' 
                  THEN ISNULL(a.FirstName,'') + ' ' + ISNULL(a.LastName,'')

                  ELSE ISNULL(a.FirstName,'') + ' ' + a.MiddleName + ' ' + ISNULL(a.LastName,'')
              END
    END AS 'CustomerName'

答案 1 :(得分:1)

另一种解决方案是:

SELECT * , 
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + replace( (  ' ' + ISNULL(a.MiddleName+' ',' '),'  ',' ') + ISNULL(a.LastName,'')
END AS 'CustomerName'