我有一张桌子' CustomerAccount'包含以下字段:
我正在尝试提取一份简单的报告,但收到错误消息。我似乎无法弄清楚出了什么问题。
我的代码:
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。
答案 0 :(得分:0)
以下是我修复它的方法:
,CASE WHEN ISNULL(CompanyName, '') = ''
THEN Cust_LastName + ', ' + Cust_FirstName
ELSE CompanyName
END COLLATE Latin1_General_CI_AI CustomerName
感谢@Aaron Bertrand,@ Paul和@DT。