我有一个字符串:'&articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13
'(不带撇号,我想将其拆分为以下值:
articleNumber variantNumber bundleNumber supplierNumber discountType
370878 (null) 3 20064 13
我能够基于'='之前的字符串提取所需的值,但是我希望有一个更优化的解决方案,一次提取所有内容,第一行是列名,第二行是值。< / p>
有可能吗? 注意:字符串可以有1、2、3、4或5对。
答案 0 :(得分:0)
类似这样的作品。可以做得更优雅。
select regexp_substr(regexp_substr('articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13'
,'(.*?)(&|$)'
,1
,1
,null
,1)
,'(.*?)(=|$)'
,2
,2
,null
,1) as articlenumber
,regexp_substr(regexp_substr('articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13'
,'(.*?)(&|$)'
,2
,2
,null
,1)
,'(.*?)(=|$)'
,2
,2
,null
,1) as variantnumber
,regexp_substr(regexp_substr('articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13'
,'(.*?)(&|$)'
,3
,3
,null
,1)
,'(.*?)(=|$)'
,2
,2
,null
,1) as bundlenumber
,regexp_substr(regexp_substr('articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13'
,'(.*?)(&|$)'
,4
,4
,null
,1)
,'(.*?)(=|$)'
,2
,2
,null
,1) as suppliernumber
,regexp_substr(regexp_substr('articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13'
,'(.*?)(&|$)'
,5
,5
,null
,1)
,'(.*?)(=|$)'
,2
,2
,null
,1) as discounttype
from dual
答案 1 :(得分:0)
尝试一下:
SET DEFINE OFF
WITH DATAA(D) AS
(SELECT '&articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13'
FROM DUAL)
SELECT SUBSTR(D,INSTR(D,'=',1,1)+1,INSTR(D,'&',1,2)-INSTR(D,'=',1,1) - 1) AS articleNumber,
SUBSTR(D,INSTR(D,'=',1,2)+1,INSTR(D,'&',1,3)-INSTR(D,'=',1,2) - 1) AS variantNumber,
SUBSTR(D,INSTR(D,'=',1,3)+1,INSTR(D,'&',1,4)-INSTR(D,'=',1,3) - 1) AS bundleNumber,
SUBSTR(D,INSTR(D,'=',1,4)+1,INSTR(D,'&',1,5)-INSTR(D,'=',1,4) - 1) AS supplierNumber,
SUBSTR(D,INSTR(D,'=',1,5)+1) AS supplierNumber
FROM DATAA;
--
Output:
--
370878 | (null) | 3 | 20064 | 13
干杯!
答案 2 :(得分:0)
SQL> with input as
2 ( select '&articleNumber=370878&variantNumber=&bundleNumber=3&supplierNumber=20064&discountType=13' text
3 from dual
4 )
5 , splitted_by_ampersand as
6 ( select regexp_substr(text,'[^&]+',1,level) text
7 from input
8 connect by level <= regexp_count(text,'&')
9 )
10 , splitted_by_equal_sign as
11 ( select substr(text,1,instr(text,'=')-1) name
12 , substr(text,instr(text,'=')+1) value
13 from splitted_by_ampersand
14 )
15 select *
16 from splitted_by_equal_sign
17 pivot ( max(value)
18 for name in
19 ( 'articleNumber' as "articleNumber"
20 , 'variantNumber' as "variantNumber"
21 , 'bundleNumber' as "bundleNumber"
22 , 'supplierNumber' as "supplierNumber"
23 , 'discountType' as "discountType"
24 )
25 )
26 /
articleNumber variantNumber bundleNumber supplierNumber discountType
-------------------- -------------------- -------------------- -------------------- --------------------
370878 3 20064 13
1 rij is geselecteerd.