【问题标题】:Trouble replacing abbreviated text correctly in SQL在 SQL 中正确替换缩写文本时遇到问题
【发布时间】:2018-09-01 09:54:38
【问题描述】:

我正在尝试用相应的详细描述替换一堆难以破译的缩写。我有一个名为 Abbreviations 的表,其中仅包含要查找的缩写列表以及应将其更改为的相应描述。此外,“已替换”表包含一个未更改的缩写描述列表,我想在一个名为“DescriptionCodes”的列中进行更改

我要更改的数据是不同茶的列表。例如条目 “TADIN H-B GR”是“TADIN HERBAL BAG GREEN”的缩写

我目前使用的 SQL 代码如下所示:

BEGIN TRANSACTION
DECLARE @Desc varchar(500)
DECLARE @Abbr varchar(500)

DECLARE contact_cursor CURSOR FOR
SELECT Description, Abbrv FROM dbo.Abbreviations

OPEN contact_cursor

FETCH NEXT FROM contact_cursor
INTO @Desc, @Abbr

WHILE @@FETCH_STATUS = 0
BEGIN
  PRINT 'Changing ' + @Abbr + ' to ' + @Desc

  UPDATE Replaced
  SET DescriptionCodes = REPLACE(DescriptionCodes, @Abbr, @Desc)
  WHERE DescriptionCodes LIKE CONCAT('% ', @Abbr, ' %')

  FETCH NEXT FROM contact_cursor
  INTO @Desc, @Abbr
END

CLOSE contact_cursor
DEALLOCATE contact_cursor

COMMIT

当然,我遇到的问题是,一旦应用了缩写,详细描述可能包含一个匹配另一个缩写的子字符串,然后应用该缩写。例如,PKG 可能更改为 PACKAGE,但 PA 也是 PINEAPPLE 的缩写,这意味着一旦应用该更改,最终结果将是 PINEAPPLECKAGE。每个缩写在两边都有一个空格,所以我想利用这个事实来不更新任何两边没有空格的缩写,因此 CONCAT('%', @Abbr, '%') 在我的代码中。但是,当我尝试这种方法时,基本上什么都没有改变。通过删除该约束,我已经能够看到有限的成功,但随后发生了另一个问题。关于如何确保只考虑和更新两侧带有空格的缩写的任何想法?

更新: 在尝试了这里发布的几个解决方案之后,我仍然无法让它工作,我不知道为什么。从表面上看,这是正确的。以下是我正在处理的一些数据的示例:

JUSTEA HBL PKG CMCL CHM LG LR  1.5OZ
PRIDE OF INDIA BG ASM B BKFST  25 CT
CTL BR H-B 7BLSM PP            1 CT
POSTI H-B HRH CRN              20 CT
DRS H-B EPGP LPLDS PTVP TGN    20 CT
ULTLC BG CHG                   100 CT
PG TIPS BG D B                 40 CT
RPBL R-B B HLDY FT BLD         6 CT

这很好,看起来应该可以毫无问题地转换。然而,当我运行带有空格的代码时(如“LIKE '%' + @Abbr + '%'”,这是我的第一个倾向),数据保持完全不变。如果我删除它们,数据将变得完全无法理解。例如以 PRIDE OF INDIA 开头的行变成了

PRIDE OF INDIA(N) IRISH AFTERNOON BLACK AG ASIA PLUM RICOT SPICE(D) EARMINT BLACK BLACK KFST 25 CURRANT AN AID N T

我觉得我应该注意这些数据最初是从 Excel 电子表格导入的。是否有可能与无法识别的空间有关?

【问题讨论】:

  • 能否提供一些查询失败的示例数据?看起来应该可以了。
  • 用示例数据更新。

标签: sql sql-server replace abbreviation


【解决方案1】:

我认为您需要多次更新 Replaced 表的每条记录,并且您需要考虑缩写的 4 个位置(Alone、First、In the middle、Last)。像这样的:

DECLARE @Replaced TABLE ([DescriptionCodes] varchar(50))
DECLARE @Abbreviations TABLE ([Abbrv] varchar(50), [Description] varchar(50))

INSERT INTO @Replaced([DescriptionCodes]) VALUES ('TADIN H-B GR')
INSERT INTO @Replaced([DescriptionCodes]) VALUES ('PKG')
INSERT INTO @Replaced([DescriptionCodes]) VALUES ('PKG PA')

INSERT INTO @Abbreviations([Abbrv], [Description]) VALUES ('H-B', 'HERBAL BAG')
INSERT INTO @Abbreviations([Abbrv], [Description]) VALUES ('GR', 'GREEN')
INSERT INTO @Abbreviations([Abbrv], [Description]) VALUES ('PKG', 'PACKAGE')
INSERT INTO @Abbreviations([Abbrv], [Description]) VALUES ('PA', 'PINAPPLE')

DECLARE @RowCount int;

WHILE 1 = 1
BEGIN
    SET @RowCount = 0;

    UPDATE r
    SET r.[DescriptionCodes] = REPLACE(r.[DescriptionCodes], a.[Abbrv], a.[Description])
    FROM @Replaced r join @Abbreviations a ON r.[DescriptionCodes] = a.[Abbrv];
    SET @RowCount = @RowCount + @@RowCount;

    UPDATE r
    SET r.[DescriptionCodes] = REPLACE(r.[DescriptionCodes], ' ' + a.[Abbrv] + ' ', ' ' + a.[Description] + ' ')
    FROM @Replaced r join @Abbreviations a ON r.[DescriptionCodes] like '% ' + a.[Abbrv] + ' %';
    SET @RowCount = @RowCount + @@RowCount;

    UPDATE r
    SET r.[DescriptionCodes] = REPLACE(r.[DescriptionCodes], ' ' + a.[Abbrv],' ' + a.[Description])
    FROM @Replaced r join @Abbreviations a ON r.[DescriptionCodes] like '% ' + a.[Abbrv];
    SET @RowCount = @RowCount + @@RowCount;

    UPDATE r
    SET r.[DescriptionCodes] = REPLACE(r.[DescriptionCodes], a.[Abbrv] + ' ', a.[Description] + ' ')
    FROM @Replaced r join @Abbreviations a ON r.[DescriptionCodes] like a.[Abbrv] + ' %';
    SET @RowCount = @RowCount + @@RowCount;

    IF @ROWCOUNT = 0 BREAK;
END

SELECT * FROM @Replaced

【讨论】:

    【解决方案2】:

    你可以这样做,它会实现你想要做的:

      LIKE '% ' + @Abbr + ' %'
    

    【讨论】:

      【解决方案3】:

      如果要添加空格,为什么要使用“LIKE”?做吧:

      WHERE DescriptionCodes = @Abbr
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 2020-03-01
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多