SQL-Server CONCAT案例

时间:2015-11-17 15:32:59

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

我有以下练习:

  

连接第一个,中间名,姓氏和名称后缀以形成   客户名称采用以下格式:FirstName [MiddleName。]   LastName [,后缀]。请注意,应省略NULL值。

我将此解释为以下场景(从图像创建表并插入一些值):

请在下面找到我的样本数据,名称是#TB_Customer enter image description here 客户名称列是预期结果,应为

格式
  • FirstName MiddleName.LastName, Suffix如果我有所有人的肠 田野。
  • MiddleName和Suffix可以是可选的,所以案例是:

  • 如果有suffix但不是MiddleName,那么CustomerName
    应该是Firstname LastName,Suffix

  • 的形式
  • 如果有MiddleName但不是suffix,那么CustomerName
    应该是FirstName MiddleName.LastName

  • 的形式
  • 如果MiddleNameSuffix都为空,则CustomerName应该为空 形式为FirstName LastName)

这就是我得到的: enter image description here

但正如您所看到的,我写的CustomerName案例查询并未按预期工作(请参阅上面的cases以及子弹)

我写的用于获取CustomerName列的查询是:

SELECT 
(case
when (MiddleName is not null and Suffix is not null)  then
CONCAT(c.FIRSTNAME,' ', c.MiddleName,'.', c.LASTNAME, ', ',Suffix)  
when (MiddleName is null and suffix is null) then 
CONCAT(c.FIRSTNAME,' ' ,c.LASTNAME) 
when (MiddleName is null and Suffix is not null )then
concat (c.FirstName, ' ', c.LastName, ', ',Suffix )  
when (Suffix is null and MiddleName is not null) then
concat (c.FirstName, ' ',MiddleName,'.',LastName)
end 
)AS CustomerName
,c.*
FROM   #TB_Customer c;

我有两个问题:

  • 我是否理解这项练习并且我有一个好的逻辑?
  • 我在哪里犯了错误以及正确的查询是什么?

使用SQL-Server 2012

修改

要重新创建我的方案,请参阅下面的代码(抱歉没有链接小提琴,但网站在我当前位置没有响应)

CREATE TABLE #TB_Customer
(
CustomerID int , --PK
Title varchar(50),
FirstName varchar(50),
MiddleName varchar(50),
LastName varchar(50),
Suffix varchar(50),
EmailAddress varchar(50),
Phone varchar(50),
Gender varchar(50),
Birthdate varchar(50),
--no fk
PRIMARY KEY (CustomerID)
)

insert into #TB_Customer values
('1','Sir','John','Jacob','Adams','St','johnJacobAdams@gmail.com','0677731235','M','1989-04-06'),
('2','Mr.','Russel','Thyrone','Peterson','pr','thyronePeterson@yahoo.com','555-010405','M','1963-02-01'),
('3','Ms.','Anne','Candice','Acola','aca','CandiceA@gmail.com','07408989989','F','1988-05-19'),
('4','Mrs.','Sophia','Veronika','Turner','tvs','SophiaVT@facebook.de','0423423887','F','1983-06-20'),
('5','Ms','Margaret','','Delafleur','','delaMarg@yahoo.co','233223355','Female','1982-02-25'),
('6','Mrs','Jessica','Luana','Cruz','','Jess@yahoo.com','787876631','Female','1922-05-05'),
('7','Mr','Dyrius','','Cruz','dc','dyr33@yahoo.com','0673332211','Male','1987-03-01')

update #TB_Customer
set Gender = 'Male' where Gender = 'M'
update #TB_Customer
set Gender = 'Female' where Gender = 'F'

4 个答案:

答案 0 :(得分:4)

这样的事也应该有效......

SELECT concat(firstname
     ,CASE WHEN ISNULL(middlename,'') <> '' THEN ' '+middlename+'.'
        WHEN ISNULL(middlename,'') <> '' AND ISNULL(suffix,'') = '' THEN '.' 
        ELSE ' ' END
     ,lastname
     ,CASE WHEN ISNULL(suffix,'') <> '' THEN ', '+suffix END)
FROM #TB_Customer

输出:

John Jacob.Adams, St
Russel Thyrone.Peterson, pr
Anne Candice.Acola, aca
Sophia Veronika.Turner, tvs
Margaret Delafleur
Jessica Luana.Cruz
Dyrius Cruz, dc
John Adams, St

答案 1 :(得分:2)

我在查询中看不到错误,我知道其中一列是否为空,其他列都是,但请尝试这种方式:

SELECT COALESCE(c.FIRSTNAME,'') + ' ' +
        CASE WHEN COALESCE(c.MiddleName,'') = ''
                THEN ''
                ELSE c.MiddleName + '.'
            END
        + COALESCE(c.LASTNAME,'') +
        CASE WHEN COALESCE(Suffix,'') = ''
                THEN ''
                ELSE  ', ' + Suffix
             END AS CustomerName, c.*

FROM   #TB_Customer c;

@Henrik是对的,''和NULL是不同的东西

答案 2 :(得分:1)

现在您已经提供了代码,我可以看到错误是什么:空字符串与NULL不同。

因此,对中间名称/后缀的存在的测试将始终为真。

将这些字段设置为NULL,或者扩充测试以检查NULL或空字符串。

答案 3 :(得分:1)

SELECT
    STUFF(RTRIM(
        CONCAT('  ',
            COALESCE(NULLIF(FirstName,'') + ' ', ''),
            COALESCE(NULLIF(MiddleName,'') + '.', ''),
            COALESCE(NULLIF(LastName,''), ''),
            COALESCE(', ' + NULLIF(Suffix,'') , '')
        )
    ), 2, 0,'')
FROM #TB_Customer tc

为了一些奇怪的原因添加STUFF包含你所拥有的只是后缀