【问题标题】:How to replace multiple matching patterns with multiple specified replacement strings in SQL - Oracle [duplicate]如何在 SQL 中用多个指定的替换字符串替换多个匹配模式 - Oracle [重复]
【发布时间】:2015-02-18 19:02:20
【问题描述】:

我们有 varchar2(250) 列,其中包含我们想要用其他特定文本替换的各种文本数据。 注意:我不想为这个需求使用多个替换功能,有没有更好的方法来做到这一点

要求是:

Replace <   with    "less than"
Replace >   With    "greater than"
Replace &   With    and

例如表中的数据是:

"amount must be < 1 & > 2" 

SQL 的输出应该是:

"amount must be greater than 1 and less than 2" 

【问题讨论】:

  • 那是一列数据吗?
  • REPLACE 有什么问题?
  • 是的,这是一列数据,解决方法请使用上面的双重和示例文本
  • 这个问题是我们需要用字符串值替换15到20个字符
  • "我不想使用多重替换功能" - 为什么?您在这里要解决的真正问题是什么?

标签: sql oracle


【解决方案1】:

据我说,您必须使用 3 个替换语句。像这样的东西:-

SELECT REPLACE( REPLACE( REPLACE ('amount must be < 1 & > 2', '<', 'less than'), '>' ,'greater than'), '&', 'and')
FROM DUAL;

我认为这只是解决方案。

【讨论】:

    【解决方案2】:

    您只需要使用 REPLACE 三次,这并不费力。

    SQL> WITH DATA AS(
      2  SELECT 'amount must be < 1 & > 2' str FROM dual
      3  )
      4  SELECT REPLACE(REPLACE(REPLACE(str, '<', 'less than'), '&','and'),'>','greater than') str
      5  FROM DATA
      6  /
    
    STR
    ---------------------------------------------
    amount must be less than 1 and greater than 2
    
    SQL>
    

    【讨论】:

      【解决方案3】:

      使用replace函数

      SELECT Replace(
                Replace(
                  Replace(a, '>', 'greater than'), 
                                    '<', 'lesser than'),
                                                 '&', 'and')
      FROM   Yourtable 
      

      DEMO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-23
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多