1

我有以下声明。我正在尝试使用 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 - 口渴饮料有限公司

我想说的是,如果账户名以字符串“COD -”开头,则必须删除该部分。

4

2 回答 2

0

使用替换语句将“COD”替换为您想要的任何值。看看这里:https ://msdn.microsoft.com/en-za/library/ms186862.aspx

于 2016-03-14T09:14:48.640 回答
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
于 2016-03-14T09:22:47.897 回答