需要帮助从字符串中提取子字符串直到一定长度之前的最后一个空格

时间:2019-03-26 09:25:03

标签: sql oracle substring

我正在尝试将字符串细分为特定长度。例如,字符串长度为100。我需要将字符串切成31个字符,而不是切掉整个单词。

PARTNER ESSENTIAL 24 MONTHS INITIAL FOR NETBACKUP SELF SERVICE XPLAT

如果我仅将后31个字符分割开,结果是第一部分为PARTNER ESSENTIAL 24 MONTHS INI,第二部分为TIAL FOR NETBACKUP SELF SERV,单词INITIAL被分割。我需要以... 24 MONTHS结尾,并将整个词INITIAL放入第二部分。

因此,我需要将字符串分隔到最后一个空格,以使其清晰易懂。需要在一个查询中执行另一件事。

3 个答案:

答案 0 :(得分:2)

您可以使用REGEXP_REPLACE。输入尽可能多的字符(最多31个),后跟一个空格或字符串的结尾。

SELECT REGEXP_REPLACE(
  'PARTNER ESSENTIAL 24 MONTHS INITIAL FOR NETBACKUP SELF SERVICE XPLAT',
  '(.{1,31})( |$)(.*)',
  '\1' )
FROM dual;
  

24个月的专家基本经验

使用'\3'来获取其余的信息:

  

网络备份自服务XPLAT的初始

答案 1 :(得分:1)

您可以使用instrsubstr来标识相关的空格位置,并带有大小写表达式,这样您就只能尝试分割较长的值:

-- CTE for sample data
with your_table (str) as (
  select 'PARTNER ESSENTIAL 24 MONTHS INITIAL FOR NETBACKUP SELF SERVICE XPLAT' from dual
  union all
  select 'PARTNER ESSENTIAL 24 MONTHS INI' from dual
  union all
  select 'PARTNER ESSENTIAL 24 MONTHS' from dual
  union all
  select 'PARTNER ESSENTIAL 24' from dual
)
-- actual query
select str,
  length(str) as str_len,
  case when length(str) <= 31 then str
       else substr(str, 1, instr(str, ' ', 31 - length(str), 1))
  end as part_1,
  case when length(str) <= 31 then null
       else substr(str, instr(str, ' ', 31 - length(str), 1) + 1)
  end as part_2
from your_table;

STR                                                                     STR_LEN PART_1                          PART_2                                                               
-------------------------------------------------------------------- ---------- ------------------------------- ---------------------------------------------------------------------
PARTNER ESSENTIAL 24 MONTHS INITIAL FOR NETBACKUP SELF SERVICE XPLAT         68 PARTNER ESSENTIAL 24 MONTHS     INITIAL FOR NETBACKUP SELF SERVICE XPLAT                             
PARTNER ESSENTIAL 24 MONTHS INI                                              31 PARTNER ESSENTIAL 24 MONTHS INI                                                                      
PARTNER ESSENTIAL 24 MONTHS                                                  27 PARTNER ESSENTIAL 24 MONTHS                                                                          
PARTNER ESSENTIAL 24                                                         20 PARTNER ESSENTIAL 24                                                                                 

答案 2 :(得分:1)

不像已经发布的regexp_replace变体那样优雅,而是另一种使用substrinstr的逐步方法。我试图使代码本身注释,让我知道是否有任何问题。 HTH KR彼得

with 
 your_input as 
    (select 'PARTNER ESSENTIAL 24 MONTHS INITIAL FOR NETBACKUP SELF SERVICE XPLAT' string_ from dual)
,first_31_chars as
    (select substr(your_input.string_,1,31) substring_ from your_input)
,last_space_in_first_31_chars as
    (select instr(first_31_chars.substring_,' ',-1,1) position_ from first_31_chars)
,first_part as 
    (select trim(substr(string_,1,position_)) fp_substring_ from your_input,last_space_in_first_31_chars)
,second_part as 
    (select trim(substr(string_,position_)) sp_substring_ from your_input,last_space_in_first_31_chars)
select fp_substring_,sp_substring_ from first_part,second_part;