用逗号连接名字,姓氏和中间名

时间:2016-10-27 09:36:42

标签: sql sql-server

我想在SQL Server中连接3个列,如下所示:

MAX(LTRIM(RTRIM((ISNULL(LastName,'') + 
    ', ' + 
    ISNULL(FirstName,'') + 
    ', ' + 
    ISNULL(MiddleName,''))))) AS FullName

我在SELECT子句中使用了此列的值:

MAX(FullName) AS FullName,

我想处理NULL值,以防所有3个姓,中间名和名字都是BLANK或NULL。上面使用的查询将显示" ,,"如果所有3列都是NULL或BLANK。但我想展示" N / A"在这种情况下。

提前致谢。

5 个答案:

答案 0 :(得分:1)

您可以使用CASE表达式:

SELECT MAX(CASE WHEN ISNULL(FirstName, '') = '' AND
                     ISNULL(MiddleName, '') = '' AND
                     ISNULL(LastName, '') = ''
                THEN 'N/A'
                ELSE LTRIM(RTRIM((ISNULL(LastName,'') +  ', ' +
                                  ISNULL(FirstName,'') + ', ' +
                                  ISNULL(MiddleName,''))))
           END) AS FullName
FROM yourTable
...

答案 1 :(得分:0)

使用Concat,如下所示,这将进行隐式转换。所以不需要使用ISNULL

select isnull(MAX(LTRIM(RTRIM((concat(LastName, 
    ', ' ,
    FirstName, 
    ', ' ,
    MiddleName,''))))) ,'n/a')AS FullName from table

答案 2 :(得分:0)

检查 COALESCE 然后 CASE声明

Declare @FirstName VARCHAR(50)='',@MiddleName VARCHAR(50),@LastName VARCHAR(50)

SELECT 
CASE WHEN ISNULL(COALESCE(@FirstName,@MiddleName,@LastName),'')<>'' 
THEN ISNULL(@FirstName,'')+',' +ISNULL(@MiddleName,'')+','+ISNULL(@LastName,'')
ELSE 'N/A' END AS FullName

答案 3 :(得分:0)

下面的方法可能看起来相当复杂,但它确实使添加或删除列变得更加简单,并且对于它所感知到的所有复杂性,它实际上并没有做太多的事情,因此不会增加太多开销。

第一步是将每个列都拆分为具有公共列名称的行,这样就可以转为

FirstName   MiddleName  LastName    
------------------------------------
A           NULL        C           

进入

Name
------
A
NULL
C

CROSS APPLY与表值构造函数VALUES

一起使用
SELECT  x.Name
FROM    (VALUES ('A', NULL,'C')) AS t (FirstName, MiddleName, LastName) 
CROSS APPLY (VALUES (1, t.FirstName), (2, t.MiddleName), (3, t.LastName)) x (SortOrder, Name)
ORDER BY x.SortOrder

然后您可以使用WHERE ISNULL(Name, '') <> ''删除NULL和空格,然后只有有效数据可以连接在一起using SQL Server's XML Extensions。所以你最终得到一个完整的查询,如:

WITH TestData AS
(   SELECT  *
    FROM (VALUES ('A'), (NULL)) AS f (FirstName)
    CROSS JOIN (VALUES ('B'), (NULL)) AS m (MiddleName)
    CROSS JOIN (VALUES ('C'), (NULL)) AS l (LastName)
)
SELECT  t.*,
        NamesConcat = ISNULL(STUFF(NamesConcat.value('.', 'NVARCHAR(MAX)'), 1, 2, ''), 'N/A')
FROM    TestData AS t
        CROSS APPLY
        (   SELECT  ', ' + x.Name
            FROM    (VALUES 
                        (1, t.FirstName), 
                        (2, t.MiddleName), 
                        (3, t.LastName)
                    ) x (SortOrder, Name)
            WHERE   ISNULL(x.Name, '') <> '' -- NOT BLANK OR NULL
            ORDER BY x.SortOrder
            FOR XML PATH(''), TYPE
        ) x (NamesConcat);

<强>结果

FirstName   MiddleName  LastName    NamesConcat
-------------------------------------------------
A           B           C           A, B, C
A           NULL        C           A, C
A           B           NULL        A, B
A           NULL        NULL        A
NULL        B           C           B, C
NULL        NULL        C           C
NULL        B           NULL        B
NULL        NULL        NULL        N/A

答案 4 :(得分:0)

  1. 选择Isnull(FirstName,'')+''+','+ Isnull(MiddleName,'')+''+','+ Isnull(姓氏,'')作为来自personaldata的名称

  2. 选择FirstName +''+','+ MiddleName +''+','+姓名来自personaldata

  3. 注意:如果所有值都存在,则第二个查询将正常工作,如果有任何值为null,则它将为Name返回null,为避免这种问题,请使用第一个查询。