简体   繁体   中英

Extract and Concatenate multiple instances of strings from column that match a regex expression

I have a dataset similar to the following dataset:

WITH CTE AS (
  SELECT ';000d6a11,Adult' AS Policy
  UNION ALL
  SELECT ';0003ga21,Child' AS Policy
  UNION ALL 
  SELECT ';000d6a11,Adult;;0003ga21,Child' AS Policy
Policy
;000d6a11,Adult
;0003ga21,Child
;000d6a11,Adult;;0003ga21,Child

I want to return the following: | Policy | PolicyNo | | -------- | -------- | |;000d6a11,Adult| 000d6a11| |;0003ga21,Child| 0003ga21| |;000d6a11,Adult;;0003ga21,Child| 000d6a11,0003ga21|

I have tried using REGEXP_EXTRACT_ALL() however this returns the PolicyNo on multiple rows and does not remove the; before the second instance of the PolicyNo:

Policy PolicyNo
;000d6a11,Adult 000d6a11
;0003ga21,Child 0003ga21
;000d6a11,Adult;;0003ga21,Child 000d6a11
;0003ga21

The query I have used is:

 WITH CTE AS (
  SELECT ';000d6a11,Adult' AS Policy
  UNION ALL
  SELECT ';0003ga21,Child' AS Policy
  UNION ALL 
  SELECT ';000d6a11,Adult;;0003ga21,Child' AS Policy
  ) SELECT Policy 
    ,REGEXP_EXTRACT_ALL(Policy, ';(.+?),')
FROM CTE

How can I achieve the desired result?

You can use

ARRAY_TO_STRING(REGEXP_EXTRACT_ALL(Policy, ';([^;,]+),'), ',')

where

  • ;([^;,]+), captures into Group 1 any one or more chars other than ; and , (excluding them from the value)
  • ARRAY_TO_STRING will create a single comma-separated string from the found matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM