如何在Oracle中拆分多个特殊字符串

hfsqlsce  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(101)

我有一个字符串与2特殊字符如下

String :'PAN~HLASD4564P|VOTER_ID~VDD3455355'

我想在2列输出如下:

ID_TYPE       VALUE
-------       ------
PAN           HLASD4564P
VOTER_ID      VDD3455355

enter image description here

aemubtdh

aemubtdh1#

您可以使用CONNECT BYREGEXP_SUBSTR如下:

SQL> WITH YOUR_DATA AS (
  2      SELECT 'PAN~HLASD4564P|VOTER_ID~VDD3455355' AS STR
  3      FROM DUAL
  4  ) -- Your query starts from here
  5  SELECT 
  6      REGEXP_SUBSTR(NEW_STR, '[^~]+', 1, 1) AS ID,
  7      REGEXP_SUBSTR(NEW_STR, '[^~]+', 1, 2) AS VALUE
  8  FROM
  9      (
 10          SELECT REGEXP_SUBSTR(STR, '[^|]+', 1, LEVEL) NEW_STR
 11          FROM YOUR_DATA
 12          CONNECT BY LEVEL <= 2
 13      );

ID         VALUE
---------- -------------
PAN        HLASD4564P
VOTER_ID   VDD3455355

SQL>
kxkpmulp

kxkpmulp2#

WITH your_data AS (
    SELECT
        'ISAT_FWASSAMBA_008' AS str
    FROM
        dual
) -- Your query starts from here
SELECT
    regexp_substr(new_str, '[^_]+', 1, 1) AS str1,
    regexp_substr(new_str, '[^_]+', 1, 2) AS str2,
    regexp_substr(new_str, '[^_]+', 1, 3) AS str3,
    regexp_substr(new_str, '[^_]+', 1, 4) AS str4
FROM
    (
        SELECT
            regexp_substr(str, '[^|]+', 1, level) new_str
        FROM
            your_data
        CONNECT BY level <= 1
    );

运转良好

相关问题