Oracle   发布时间:2022-05-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了oralce逗号分割变多行大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

方法一

select a.*,

REGEXP_SUBSTR(a.rolecode,'[^,]+',1,l) AS rolecode

from p_user a,(SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=100) b

WHERE l <=LENGTH(a.rolecode) - LENGTH(REPLACE(rolecode,','))+1

使用函数REGEXP_SUBSTR拆分字符串:

5个参数

第一个是输入的字符串

第二个是正则表达式

第三个是标识从第几个字符开始正则表达式匹配。(默认为1)

第四个是标识第几个匹配组。(默认为1)

第五个是是取值范围:

i:大小写不敏感;

c:大小写敏感;

n:点号 . 不匹配换行符号;

m:多行模式;

x:扩展模式,忽略正则表达式中的空白字符。

SELECT a.*,REGEXP_SUBSTR(servicereqID,'[^;]+',l) AS servicereq

FROM sum_portal_satisfaction a,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">WHERE l <=LENGTH(servicereqID) - LENGTH(REPLACE(servicereqID,';'))+1

ORDER BY 1,2;

----SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=100; 生成1到100的数据行。

----l <=LENGTH(servicereqID) - LENGTH(REPLACE(servicereqID,';'))+1,注意此处是‘L’并非‘1’,上面的REGEXP_SUBSTR的第四个参数也一样。

---下面为拆分字符串,再进行的行转列

create or replace vIEw v_sum_portal_satisfaction_sr as

select

survey_type,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">survey_time,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">center_code,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">center_name,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">city_ID,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">city_name,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">REGEXP_SUBSTR(servicereqID,l) AS servicereqID,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">REGEXP_SUBSTR(servicereqname,l) AS servicereqname,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">sum(decode(survey_value,sur_times,null)) giveup_times,--调查值 -1:未处理 0:用户放弃 1:很满意 2.满意 3.对csr不满意 4.对其它不满意

sum(sur_times) sur_times

group by

subslevelID,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">servicereqID,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;">servicereqname,l

方法二:

create table testtable (

ID nvarchar2(200) primary key not null,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;"> content nvarchar2(200) not null

)

insert into testtable values ('4','馆内IDx_10馆外IDx_11总体IDx_12');

select * from table ( CAST (fn_split(('馆内IDx_1$馆外IDx_2$总体IDx_3$'),'$') as ty_str_split ) )

select * from testtable b left join table (fn_split((content),'$') ) a on 1=1;

--实现split函数

CREATE OR REPLACE TYPE ty_str_split IS table OF VARCHAR2 (4000);

CREATE OR REPLACE FUNCTION fn_split (p_str IN VARCHAR2,p_delimiter IN VARCHAR2)

RETURN ty_str_split

IS

j INT := 0;

i INT := 1;

len INT := 0;

len1 INT := 0;

str VARCHAR2 (4000);

str_split ty_str_split := ty_str_split ();

BEGIN

len := LENGTH (p_str);

len1 := LENGTH (p_delimiter);

WHILE j < len

LOOP

j := INSTR (p_str,p_delimiter,i);

IF j = 0

THEN

j := len;

str := SUBSTR (p_str,51);Font-family:Simsun;Font-size:14px;Font-style:normal;Font-variant:normal;Font-weight:400;letter-spacing:normal;List-style-image:none;List-style-position:outsIDe;List-style-type:none;margin-left:0px;text-align:left;text-decoration:none;text-indent:28px;text-transform:none;word-spacing:0px;"> str_split.EXTEND;

str_split (str_split.COUNT) := str;

IF i >= len

THEN

EXIT;

END IF;

ELSE

i := j + len1;

END IF;

END LOOP;

RETURN str_split;

END fn_split;

DECLARE

CURSOR c

IS

r c%rOWTYPE;

OPEN c;

FETCH c INTO r;

EXIT WHEN c%NOTFOUND;

DBMS_OUTPUT.put_line (r.column_value);

CLOSE c;

END;

大佬总结

以上是大佬教程为你收集整理的oralce逗号分割变多行全部内容,希望文章能够帮你解决oralce逗号分割变多行所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: