我有以下声明。我正在尝试使用CASE函数来删除" COD - "在以" COD - "
开头的任何帐户名称前面SELECT
DCLink
, CASE
WHEN LEFT(Client.Account, 6) = 'COD - ' THEN LTRIM(Client.Account)
ELSE Client.Account
END AS CustName
, Client.Name AS AccName
, Client.Contact_Person
, Client.Telephone AS Telephone_1
, Client.Telephone2 AS Telephone_2
, Client.Fax1 AS Fax_1
, Client.Fax2 AS Fax_2
, Client.Tax_Number AS VATNumber
, Client.EMail
, SalesRep.Code
, SalesRep.Name
, CONCAT(Client.Physical1, ' ', Client.Physical2, ' ', Client.Physical3, ' ', Client.Physical4, ' ', Client.Physical5, ' ', Client.PhysicalPC) AS CustPhysical
, CONCAT(Client.Post1, ' ', Client.Post2, ' ', Client.Post3, ' ', Client.Post4, ' ', Client.Post5, ' ', Client.PostPC) AS CustPost
FROM Client
INNER JOIN SalesRep ON SalesRep.idSalesRep = Client.RepID
我觉得我在这里错过了一个简单的步骤?
换句话说,在数据库中可能存在以下两个客户帐户名称:
我想说,如果帐户名称以字符串" COD - "开头,则必须删除该部分。
答案 0 :(得分:0)
使用替换语句将“COD”替换为您想要的任何值。 看看这里:https://msdn.microsoft.com/en-za/library/ms186862.aspx
答案 1 :(得分:0)
SELECT
DCLink
, REPLACE(Client.Account,'COD - ','') AS CustName
, Client.Name AS AccName
, Client.Contact_Person
, Client.Telephone AS Telephone_1
, Client.Telephone2 AS Telephone_2
, Client.Fax1 AS Fax_1
, Client.Fax2 AS Fax_2
, Client.Tax_Number AS VATNumber
, Client.EMail
, SalesRep.Code
, SalesRep.Name
, CONCAT(Client.Physical1, ' ', Client.Physical2, ' ', Client.Physical3, ' ', Client.Physical4, ' ', Client.Physical5, ' ', Client.PhysicalPC) AS CustPhysical
, CONCAT(Client.Post1, ' ', Client.Post2, ' ', Client.Post3, ' ', Client.Post4, ' ', Client.Post5, ' ', Client.PostPC) AS CustPost
FROM Client
INNER JOIN SalesRep ON SalesRep.idSalesRep = Client.RepID