提取第 n 个子串 [英] Extract nth substring

查看:43
本文介绍了提取第 n 个子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从字符串中提取子字符串.下面给出了可用的 ID.

I need to extract a sub string from a string. Given below are the IDs that are available.

0234-RDRT-RS111-M-EU

0234-RDRT-RS111-M-EU

0234-RDRT-RSD123-M-EU

0234-RDRT-RSD123-M-EU

我需要将突出显示的部分提取到一列中.如何将所有字符提取到 Microsoft SQL Server 中的同一列.

I need to extract the highlighted ones to a column. How do I extract all the characters to the same column in Microsoft SQL Server.

推荐答案

REGEX_SUBSTR 的第四个参数是被调用的occurence.你只需要为每一列设置你想看到的出现次数:

The fourth parameter of REGEX_SUBSTR is the called occurence. You just have to set the occurence you want to see for each column:

CREATE TABLE T (id varchar2(30));
INSERT INTO T VALUES ('0234-RDRT-RS111-M-EU');
INSERT INTO T VALUES ('0234-RDRT-RSD123-M-EU');

SELECT regexp_substr(id,'[^-]+',1,1) as col1,
       regexp_substr(id,'[^-]+',1,2) as col2,
       regexp_substr(id,'[^-]+',1,3) as col3,
       regexp_substr(id,'[^-]+',1,4) as col4,
       regexp_substr(id,'[^-]+',1,5) as col5
  FROM t;

COL1    COL2    COL3    COL4    COL5
0234    RDRT    RS111   M   EU
0234    RDRT    RSD123  M   EU

参见 REGEX_SUBSTR有关更多详细信息,请参阅 Oracle 文档.

See REGEX_SUBSTR in Oracle's documentation for more details.

这篇关于提取第 n 个子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆