0

我的输入模式如:

WITH data_tab AS (
  SELECT '1540_INPUTTER' user_name FROM  dual 
  UNION SELECT '1540_RAZZ25_UNKNOWN' FROM  dual
  UNION SELECT '1540_RAKIB17_OS_WIN10' FROM  dual
  )
SELECT REGEXP_SUBSTR(user_name,…………………….....) AS st_user_name from data_tab

期望的输出:

ST_USER_NAME
------------
INPUTTER
RAZZ25
RAKIB17
4

4 回答 4

1

一种方法是

WITH data_tab AS (
    SELECT '1540_INPUTTER' user_name FROM  dual 
    UNION SELECT '1540_RAZZ25_UNKNOWN' FROM  dual
    UNION SELECT '1540_RAKIB17_OS_WIN10' FROM  dual
)
SELECT REGEXP_SUBSTR(user_name,'_([^_]*)', 1, 1, 'i', 1) AS st_user_name 
FROM data_tab;
于 2017-03-07T06:55:59.900 回答
0

另一种方法是定义字符串的完整结构并提取第二组:

WITH data_tab AS (
  SELECT '1540_INPUTTER' user_name FROM  dual
  UNION SELECT '1540_RAZZ25_UNKNOWN' FROM  dual
  UNION SELECT '1540_RAKIB17_OS_WIN10' FROM  dual
  )
SELECT REGEXP_SUBSTR(user_name,'(\d{4}_)([A-Z0-9]+)(_)?(\w+)?',1,1,'i',2)

AS st_user_name FROM data_tab;

于 2017-03-07T07:01:43.187 回答
0

检查这个。

WITH data_tab AS (
  SELECT '1540_INPUTTER' user_name FROM  dual 
  UNION SELECT '1540_RAZZ25_UNKNOWN' FROM  dual
  UNION SELECT '1540_RAKIB17_OS_WIN10' FROM  dual
  )
SELECT

case when INSTR(SUBSTR(user_name, INSTR(user_name, '_')+1, length(user_name)- INSTR(user_name, '_')+1 ),'_') =0 then

SUBSTR(user_name, INSTR(user_name, '_')+1, length(user_name)- INSTR(user_name, '_')+1 )

else

substr((SUBSTR(user_name, INSTR(user_name, '_')+1, length(user_name)- INSTR(user_name, '_')+1 )), 1, INSTR(SUBSTR(user_name, INSTR(user_name, '_')+1, length(user_name)- INSTR(user_name, '_')+1 ),'_') -1)

end as user_name

from data_tab
于 2017-03-07T07:17:08.117 回答
-1

这是一种使用标准字符串操作(没有正则表达式,更耗时)和处理各种特殊情况(参见附加测试数据)的经济方式。

with data_tab as (
       select '1540_INPUTTER'        user_name from dual union all
       select '1540_RAZZ25_UNKNOWN'            from dual union all
       select '1540_RAKIB17_OS_WIN10'          from dual union all
       select 'MATHGUY_'                       from dual union all
       select 'NOUNDERSCORES'                  from dual
     )
select user_name,
       substr(user_name, nullif(instr(user_name, '_'), 0) + 1,
              instr(user_name || '_', '_', 1, 2) - instr(user_name, '_') - 1) 
                                                                      as st_user_name
from   data_tab
;

USER_NAME             ST_USER_NAME
--------------------- ------------
1540_INPUTTER         INPUTTER
1540_RAZZ25_UNKNOWN   RAZZ25
1540_RAKIB17_OS_WIN10 RAKIB17
MATHGUY_
NOUNDERSCORES
于 2017-03-07T16:30:23.690 回答