regexp_substr 跳过空位置

regexp_substr skips over empty positions(regexp_substr 跳过空位置)
本文介绍了regexp_substr 跳过空位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码返回管道分隔字符串中的第 n 个值...

With this code to return the nth value in a pipe delimited string...

regexp_substr(int_record.interfaceline, '[^|]+', 1, i)

当所有值都存在时它工作正常

it works fine when all values are present

Mike|Male|Yes|20000|Yes 所以 3rd 值为 Yes(正确)

Mike|Male|Yes|20000|Yes so the 3rd value is Yes (correct)

但是如果字符串是

Mike|Male||20000|Yes,第三个值是20000(不是我想要的)

Mike|Male||20000|Yes, the 3rd value is 20000 (not what I want)

如何告诉表达式不要跳过空值?

How can I tell the expression to not skip over the empty values?

TIA

迈克

推荐答案

好的.这应该是最适合您的解决方案.

OK. This should be the best solution for you.

SELECT
      REGEXP_REPLACE ( 'Mike|Male||20000|Yes',
                    '^([^|]*|){2}([^|]*).*$',
                    '2' )
          TEXT
FROM
      DUAL;

所以对于你的问题

SELECT
      REGEXP_REPLACE ( INCOMINGSTREAMOFSTRINGS,
                    '^([^|]*|){N-1}([^|]*).*$',
                    '2' )
          TEXT
FROM
      DUAL;

--INCOMINGSTREAMOFSTRINGS 是带分隔符的完整字符串

--INCOMINGSTREAMOFSTRINGS is your complete string with delimiter

--你应该通过n-1来获得第n个位置

--You should pass n-1 to obtain nth position

替代 2:

WITH T AS (SELECT 'Mike|Male||20000|Yes' X FROM DUAL)
SELECT
      X,
      REGEXP_REPLACE ( X,
                    '^([^|]*).*$',
                    '1' )
          Y1,
      REGEXP_REPLACE ( X,
                    '^[^|]*|([^|]*).*$',
                    '1' )
          Y2,
      REGEXP_REPLACE ( X,
                    '^([^|]*|){2}([^|]*).*$',
                    '2' )
          Y3,
      REGEXP_REPLACE ( X,
                    '^([^|]*|){3}([^|]*).*$',
                    '2' )
          Y4,
      REGEXP_REPLACE ( X,
                    '^([^|]*|){4}([^|]*).*$',
                    '2' )
          Y5
FROM
      T;

替代 3:

SELECT
      REGEXP_SUBSTR ( REGEXP_REPLACE ( 'Mike|Male||20000|Yes',
                                '|',
                                ';' ),
                   '(^|;)([^;]*)',
                   1,
                   1,
                   NULL,
                   2 )
          AS FIRST,
      REGEXP_SUBSTR ( REGEXP_REPLACE ( 'Mike|Male||20000|Yes',
                                '|',
                                ';' ),
                   '(^|;)([^;]*)',
                   1,
                   2,
                   NULL,
                   2 )
          AS SECOND,
      REGEXP_SUBSTR ( REGEXP_REPLACE ( 'Mike|Male||20000|Yes',
                                '|',
                                ';' ),
                   '(^|;)([^;]*)',
                   1,
                   3,
                   NULL,
                   2 )
          AS THIRD,
      REGEXP_SUBSTR ( REGEXP_REPLACE ( 'Mike|Male||20000|Yes',
                                '|',
                                ';' ),
                   '(^|;)([^;]*)',
                   1,
                   4,
                   NULL,
                   2 )
          AS FOURTH,
      REGEXP_SUBSTR ( REGEXP_REPLACE ( 'Mike|Male||20000|Yes',
                                '|',
                                ';' ),
                   '(^|;)([^;]*)',
                   1,
                   5,
                   NULL,
                   2 )
          AS FIFTH
FROM
      DUAL;

这篇关于regexp_substr 跳过空位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)