【问题标题】:How to search for the All the numbers after special character "|" using Oracle如何搜索特殊字符“|”后的所有数字使用甲骨文
【发布时间】:2021-09-06 02:24:32
【问题描述】:

基本上,我很少有记录在具有特殊字符的字段中,通常如下所示:

id      Content
1         1|1232
2         23|12323
3         33|233223

我想编写一个查询,选择管道“|”右侧的数字

所以结果应该如下: 查询结果

1   =   1232
2   =   12323

下...

【问题讨论】:

    标签: sql oracle oracle11g substring


    【解决方案1】:

    你可以使用简单的字符串函数:

    SELECT id,
           SUBSTR( content, 1, INSTR( content, '|' ) - 1 ) AS before_pipe,
           SUBSTR( content, INSTR( content, '|' ) + 1 ) AS after_pipe
    FROM   table_name
    

    或者,使用正则表达式:

    SELECT id,
           REGEXP_SUBSTR( content, '^\d+' ) AS before_pipe,
           REGEXP_SUBSTR( content, '\d+$' ) AS after_pipe
    FROM   table_name
    

    其中,对于样本数据:

    CREATE TABLE table_name ( id, content ) AS
    SELECT 1, '1|1232' FROM DUAL UNION ALL
    SELECT 2, '23|12323' FROM DUAL UNION ALL
    SELECT 3, '33|233223' FROM DUAL;
    

    两个输出:

    ID BEFORE_PIPE AFTER_PIPE
    1 1 1232
    2 23 12323
    3 33 233223

    db小提琴here

    【讨论】:

      【解决方案2】:

      根据样本数据,看看这两个选项(substr + instr 和正则表达式)是否有帮助。第 1 - 5 行中的示例数据,查询从第 6 行开始。

      SQL> with test (id, content) as
        2    (select 1, '1|1232'    from dual union all
        3     select 2, '23|12323'  from dual union all
        4     select 3, '33|233223' from dual
        5    )
        6  select id,
        7    content,
        8    --
        9    substr(content, instr(content, '|') + 1) result_1,
       10    regexp_substr(content, '\d+$') result_2
       11  from test;
      
              ID CONTENT   RESULT_1        RESULT_2
      ---------- --------- --------------- ---------------
               1 1|1232    1232            1232
               2 23|12323  12323           12323
               3 33|233223 233223          233223
      
      SQL>
      

      【讨论】:

        猜你喜欢
        • 2011-04-09
        • 1970-01-01
        • 2016-05-30
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多