【问题标题】:How to replace specific word on a large text in a column using Oracle如何使用Oracle替换列中大文本上的特定单词
【发布时间】:2021-11-10 01:25:41
【问题描述】:

我正在寻找替换特定列中的值。 例如以下列值

desc
----------
Test1 words = 30 have so much words = 20 over 60 words +
Test2 words = 30 have so much words = 20 over 60 words +
Test3 words = 30 have so much words = 20 over 60 words +
Test4 words = 30 have so much words = 20 over 60 words +

应该是(将 ords = 20 替换为 Alpha = 20)

结果:

desc
----------
Test1 words = 30 have so much Alpha = 20 over 60 words +
Test2 words = 30 have so much Alpha = 20 over 60 words +
Test3 words = 30 have so much Alpha = 20 over 60 words +
Test4 words = 30 have so much Alpha = 20 over 60 words +

我尝试了以下方法:

更新表集 desc = REPLACE('words = 20', 'words', 'Alpha') where ;

这会导致:

Alpha = 20 而不是整行。 但是,我怎样才能保留文本的其余部分只是更改“words = 20”但仍保留其余的测试?

【问题讨论】:

    标签: sql oracle11g sql-update


    【解决方案1】:

    您的陈述:

    update table set desc = REPLACE('words = 20', 'words', 'Alpha') where ...;
    

    正在替换固定字符串中的“words” 'words = 20`;它根本不会作用于您现有的列值。

    你可以这样做:

    update table set desc = REPLACE(desc, 'words = 20', 'Alpha = 20') where ...;
    

    db<>fiddle(使用有效的表名和列名)。

    如果您想替换 所有 出现的“单词”,而不仅仅是“单词 = 20”,则从两个参数中删除 = 20;但从您的尝试来看,我认为您希望它更有针对性。

    【讨论】:

      【解决方案2】:

      您需要对原始字符串执行replace,而不是在`'words = 20':

      UPDATE mytable
      SET    desc = REPLACE(desc, 'words', 'Alpha')
      

      旁注:
      我猜这些不是您表中的真实姓名,但desc 是 SQL 中的保留字。如果确实是列名,则需要用双引号将其转义(即"desc")。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        • 2014-05-20
        • 2014-07-29
        • 1970-01-01
        • 2012-01-27
        相关资源
        最近更新 更多