首页 > 解决方案 > 如何在Oracle sql中剪切字符串并返回子字符串

问题描述

我的项目中有一个要求,我需要拆分 url 并从中提取变量,url 是其中一个表中的一列。网址看起来像这样

输入行

"http://example.com/wps/myportal/example/viewer?mstrContentType=Report&   mstrId=15F4AC9C4453E5F75456438F732F7B8C&contentName=7.++CTI+Session+Order+Counts&proj=System+Xr+-+DMDR/CW&workspaceName=&woId=&vendorId=Microstrategy"

我需要 o/p 如下与目标列进行比较如下

ContentType=Report
mstrId=15F4AC9C4453E5F75456438F732F7B8C
contentName=7.++CTI+Session+Order+Counts
etc...

我只能使用 Oracle ,不应该使用编程语言。

谢谢。

标签: sqlregexoracle

解决方案


这里所有你需要使用的,如下所示的 REGEXP_SUBSTR。

create table test1(col1 varchar2(4000));
insert into test1 values(
'http://example.com/wps/myportal/example/viewer?mstrContentType=Report&   mstrId=15F4AC9C4453E5F75456438F732F7B8C&contentName=7.++CTI+Session+Order+Counts&proj=System+Xr+-+DMDR/CW&workspaceName=&woId=&vendorId=Microstrategy'
);

select regexp_substr(col1,'[^?"]+',1,2) ss from test1;

select trim(regexp_substr(regexp_substr(col1,'[^?"]+',1,2),'[^&]+', 1, level)) as output_data from test1
connect by regexp_substr(regexp_substr(col1,'[^?"]+',1,2), '[^&]+', 1, level) is not null;

输出数据

mstrContentType=Report
mstrId=15F4AC9C4453E5F75456438F732F7B8C
contentName=7.++CTI+Session+Order+Counts
proj=System+Xr+-+DMDR/CW
workspaceName=
woId=
vendorId=Microstrategy

推荐阅读