我有以下练习:



我将其解释为以下情况(从图像创建表并插入一些值):

请在下面找到我的示例数据,名称为#TB_Customer
sql - SQL Server CONCAT案例-LMLPHP
CustomerName是预期的结果,应采用以下格式

  • FirstName MiddleName.LastName, Suffix(如果我对所有人都有肠)
    田野。
  • MiddleName和Suffix可以是可选的,因此情况是:
  • 如果存在suffix但没有MiddleName,则CustomerName应采用Firstname LastName,Suffix
  • 的形式
  • 如果存在MiddleName但没有suffix,则CustomerName应采用FirstName MiddleName.LastName
  • 的形式
  • 如果MiddleNameSuffix均为null,则CustomerName应该
    格式为FirstName LastName)

  • 这就是我得到的:
    sql - SQL Server CONCAT案例-LMLPHP

    但是正如您所看到的,我编写的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

    编辑

    要重新创建我的场景,请参见下面的代码(抱歉,没有链接 fiddle ,但网站在我当前的位置没有响应)
    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','[email protected]','0677731235','M','1989-04-06'),
    ('2','Mr.','Russel','Thyrone','Peterson','pr','[email protected]','555-010405','M','1963-02-01'),
    ('3','Ms.','Anne','Candice','Acola','aca','[email protected]','07408989989','F','1988-05-19'),
    ('4','Mrs.','Sophia','Veronika','Turner','tvs','[email protected]','0423423887','F','1983-06-20'),
    ('5','Ms','Margaret','','Delafleur','','[email protected]','233223355','Female','1982-02-25'),
    ('6','Mrs','Jessica','Luana','Cruz','','[email protected]','787876631','Female','1922-05-05'),
    ('7','Mr','Dyrius','','Cruz','dc','[email protected]','0673332211','Male','1987-03-01')
    
    update #TB_Customer
    set Gender = 'Male' where Gender = 'M'
    update #TB_Customer
    set Gender = 'Female' where Gender = 'F'
    

    最佳答案

    这样的事情也应该起作用...

    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
    

    关于sql - SQL Server CONCAT案例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33760842/

    10-10 23:29