无法解决SQL Server 2014中SELECT语句中第3列的排序规则冲突

时间:2018-05-09 19:59:38

标签: sql sql-server collation

我有一张桌子' CustomerAccount'包含以下字段:

  1. 账户号码
  2. AmountDue
  3. 公司名称
  4. Cust_FirstName
  5. Cust_LastName
  6. 地址
  7. 我正在尝试提取一份简单的报告,但收到错误消息。我似乎无法弄清楚出了什么问题。

    我的代码:

    SELECT AccountNumber
          ,AmountDue
          --,ISNULL(Cust_LastName, '') + ', ' + ISNULL(Cust_FirstName, '') CustomerName
          ,CASE WHEN ISNULL(CompanyName, '') = '' 
                THEN Cust_LastName + ', ' + Cust_FirstName
                ELSE CompanyName
           END CustomerName
           -- Above: This is the line where it gives an error.  
           -- If there is no Company Name then give the Last and First name of the customer.
    
    
          ,CASE WHEN ISNULL(CompanyName, '') = '' 
                THEN ''
                ELSE Cust_FirstName + ' ' + Cust_LastName
           END Attn_To
          -- Above: If there IS a Company Name then give the First and Last name of the customer.
    
          ,[Address]
    
    FROM CustomerAccount
    

    错误讯息:

      

    无法解决SELECT语句中第3列的排序规则冲突。

    我尝试了什么:

      ,CASE WHEN ISNULL(CompanyName, '') = '' 
            THEN ''  --Cust_LastName + ', ' + Cust_FirstName
            ELSE CompanyName
       END CustomerName
     -- This works but only gives a CompanyName when there is one.
     -- But does not give me the customer name when there is no CompanyName
    

    如果我只尝试,ISNULL(Cust_LastName, '') + ', ' + ISNULL(Cust_FirstName, '')而不是CASE声明,那么它可以正常工作。我得到了客户的LastName,FirstName。

1 个答案:

答案 0 :(得分:0)

以下是我修复它的方法:

  ,CASE WHEN ISNULL(CompanyName, '') = '' 
        THEN Cust_LastName + ', ' + Cust_FirstName
        ELSE CompanyName
   END COLLATE Latin1_General_CI_AI CustomerName 

感谢@Aaron Bertrand,@ Paul和@DT。