我在列中有以下值的表。
cn=Admin,CN=Alerts,OU=App Entitlements,OU=Groups,DC=example,DC=com
cn=User,CN=Voice,OU=App Entitlements,OU=Groups,DC=example,DC=com
cn=None,CN=Call,OU=App Entitlements,OU=Groups,DC=example,DC=com
我正在尝试编写可以在second = character和second,character之间获取值的查询。像,
Alerts
Voice
Call
我写了一个几乎正常工作的查询,但最后却没有落后。
select substr(regexp_substr('cn=Admin,CN=Alerts,OU=App Entitlements,OU=Groups,DC=example,DC=com','[^,]+,',1,2),4) from dual
上述查询的输出显示为
Alerts,
但最后我不需要。我尝试使用substr方法,但它搞乱了查询。看起来我错过了简单的事情。任何人都可以建议正确的输出吗?
答案 0 :(得分:2)
为什么不尝试
select substr(regexp_substr('cn=Admin,CN=Alerts,OU=AppEntitlements,OU=Groups,DC=example,DC=com','[^,]+',1,2),4) from dual
区别在于你有'[^,]+,'
我有'[^,]+'
。
默认情况下,正则表达式是贪婪,因此它应该有效。
答案 1 :(得分:2)
查询1 - 只需获取第二个属性值:
WITH Certificates( id, CertificateString ) AS (
SELECT 1, 'cn=Admin,CN=Alerts,OU=App Entitlements,OU=Groups,DC=example,DC=com' FROM DUAL UNION ALL
SELECT 2, 'cn=User,CN=Voice,OU=App Entitlements,OU=Groups,DC=example,DC=com' FROM DUAL UNION ALL
SELECT 3, 'cn=None,CN=Call,OU=App Entitlements,OU=Groups,DC=example,DC=com' FROM DUAL
)
SELECT ID,
REGEXP_SUBSTR( CertificateString,'([^,]+)=([^,]+)',1,2,NULL,2) AS VALUE
FROM Certificates c
<强>结果:
ID VALUE
---------- -------
1 Alerts
2 Voice
3 Call
查询2 - 获取所有属性和值:
WITH Certificates( id, CertificateString ) AS (
SELECT 1, 'cn=Admin,CN=Alerts,OU=App Entitlements,OU=Groups,DC=example,DC=com' FROM DUAL UNION ALL
SELECT 2, 'cn=User,CN=Voice,OU=App Entitlements,OU=Groups,DC=example,DC=com' FROM DUAL UNION ALL
SELECT 3, 'cn=None,CN=Call,OU=App Entitlements,OU=Groups,DC=example,DC=com' FROM DUAL
)
SELECT ID,
REGEXP_SUBSTR( CertificateString,'([^,]+)=([^,]+)',1,COLUMN_VALUE,NULL,1) AS PROPERTY,
REGEXP_SUBSTR( CertificateString,'([^,]+)=([^,]+)',1,COLUMN_VALUE,NULL,2) AS VALUE
FROM Certificates c,
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT( c.CertificateString,'([^,]+?)=([^,]+)(,|$)' )
)
AS SYS.ODCINUMBERLIST
)
);
<强>结果:
ID PROPERTY VALUE
---------- -------- --------------------
1 cn Admin
1 CN Alerts
1 OU App Entitlements
1 OU Groups
1 DC example
1 DC com
2 cn User
2 CN Voice
2 OU App Entitlements
2 OU Groups
2 DC example
2 DC com
3 cn None
3 CN Call
3 OU App Entitlements
3 OU Groups
3 DC example
3 DC com