暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle SUBSTR-动态拆分字符串。

ASKTOM 2019-07-11
428

问题描述

嗨,汤姆


我需要将字符串动态拆分为4个部分,每个部分最多22个字符,但是如果第22个字符不是正确的中断 (空格),则应考虑最后一个空格。

示例: '1100新月PkWay公寓101套房200'
22个字符将是 “1100新月PkWay Ap”,那么我需要它
第一部分: 1100新月PkWay
第二: 公寓101套房
第三: 200
第四: 空

我尝试使用REGEXP_INSTR并循环使用空间计数,但寻找更好的方法。

谢谢
P

专家解答

这里有一种方法来解决这个问题:

-将字符串拆分为单词,每行一个单词
-使用避免listagg溢出的技术,将它们重新组合为长达22个字符的字符串。您可以在以下位置找到讨论的内容:

https://blogs.oracle.com/datawarehousing/managing-overflows-in-listagg
https://stewashton.wordpress.com/2019/07/10/making-lists/

这看起来像:

with val as ( 
  select '1100 Crescent PkWay Apartment 101 suite 200' str
  from   dual
), words as (
  select regexp_substr(str, '[^ ]+', 1, level) w
  from   val
  connect by regexp_substr(str, '[^ ]+', 1, level) is not null
), grps as (
  select * from words 
  match_recognize (
    measures 
      match_number () grp,
      count ( text.* ) word#
    all rows per match
    pattern ( init text* )
    define 
      text as sum ( length ( w ) ) + count ( text.* ) <= 22
  )
)
  select listagg ( w, ' ' ) 
           within group ( order by word# ) split_strs
  from   grps
  group  by grp
  order  by grp;

SPLIT_STRS            
1100 Crescent PkWay    
Apartment 101 suite    
200  

文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论