1

今天早些时候我问了一个类似的问题,但我意识到我的逻辑有一些缺陷,因为我做了一些假设。

我正在尝试做的是消除某些单词出现在字符串中......为了方便起见,我创建了一个带有一些示例的表格

create TABLE #BrokerNameT (BrokerName varchar(100))

INSERT INTO #BrokerNameT (BrokerName)
VALUES ('Morgan Stanley Co Cash'),
       ('Citi Group Algo Cash'),
       ('JP Morgan Algo'), ('JP Morgan Cash')


SELECT 
   CASE WHEN BrokerName LIKE '%CASH%' 
          THEN RTRIM(LTRIM(SUBSTRING(BrokerName, 1, CHARINDEX('Cash', BrokerName)-1)))
        WHEN BrokerName LIKE '%ALGO%' 
          THEN RTRIM(LTRIM(SUBSTRING(BrokerName, 1, CHARINDEX('algo', BrokerName)-1)))
        WHEN BrokerName LIKE '%Prog%' 
          THEN RTRIM(LTRIM(SUBSTRING(BrokerName, 1, CHARINDEX('prog', BrokerName)-1)))
        WHEN BrokerName LIKE '%BSE%' 
          THEN RTRIM(LTRIM(SUBSTRING(BrokerName, 1, CHARINDEX('bse', BrokerName)-1)))
        WHEN BrokerName LIKE '%FX%' 
          THEN RTRIM(LTRIM(SUBSTRING(BrokerName, 1, CHARINDEX('fx', BrokerName)-1)))
        WHEN BrokerName LIKE '%OTC%' 
          THEN RTRIM(LTRIM(SUBSTRING(BrokerName, 1, CHARINDEX('otc', BrokerName)-1)))
        ELSE '' 
    END
FROM #BrokerNameT

drop table #BrokerNameT

如您所见,有一个我不想在我的专栏中显示的单词列表。这个查询逻辑的问题是,如果它有两个词是不允许的:让我们说 'Citi Group Algo Cash' ;它会在找到 CASH 并仍然保持“算法”时读取 CASE 逻辑——如果你运行该查询,你会明白我的意思......

有任何想法吗?

4

1 回答 1

1

你有没有尝试过:

create TABLE #BrokerNameT (BrokerName varchar(100))
INSERT INTO #BrokerNameT (BrokerName) VALUES('Morgan Stanley Co Cash')
INSERT INTO #BrokerNameT (BrokerName) VALUES('Citi Group Algo Cash')
INSERT INTO #BrokerNameT (BrokerName) VALUES('JP Morgan Algo')
INSERT INTO #BrokerNameT (BrokerName) VALUES('JP Morgan Cash')

select
         replace(replace(replace(replace(replace(replace(BrokerName,'CASH',''),'ALGO',''),'Prog',''),'bse',''),'FX',''),'OTC','')
from #BrokerNameT

drop table #BrokerNameT

为您不想包含的每个单词添加一个新的替换。

于 2013-06-14T20:20:38.703 回答