How to split a string with special text or particular text from SQL column?

RAVITEJASRIDASYAM-3196 21 Reputation points
2023-04-23T01:45:22.7133333+00:00
  • I have a column with below data which contains multiple set of tags that starting with "tl:e1:"
  • How can i split the data that contains tl:e1: into separate column.
SQL Server Other
{count} votes

Accepted answer
  1. Alexander Petree 150 Reputation points
    2023-04-23T01:50:26.6633333+00:00

    tl:Namespace:iqqngress, tl:ImageName:text1/54445t6, tl:collection:stg-r2-pksa, tl:cmdb:AppID:6100762427, tl:collection:Privileged_Policy, tl:collection:prd-lv-v1-pksa, tl:Namespace:ingress-udp, tl:collection:prd-atl-a1-pks, tl:collection:dev-r2-pksa, tl:ImageName:text1/54445t6, tl:collection:nre-npdp1-pksa


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-04-24T03:05:56.8533333+00:00

    Hi @RAVITEJASRIDASYAM-3196

    You can try this query.

    create table test(col varchar(max));
    insert into test values('tl:Namespace:iqqngress, tl:ImageName:text1/54445t6, tl:collection:stg-r2-pksa, tl:cmdb:AppID:6100762427, tl:collection:Privileged_Policy, tl:collection:prd-lv-v1-pksa, tl:Namespace:ingress-udp, tl:collection:prd-atl-a1-pks, tl:collection:dev-r2-pksa, tl:ImageName:text1/54445t6, tl:collection:nre-npdp1-pksa')
    
    
    ;with T1 as(
      select value from test cross apply string_split(col,',')
    ),T2 as(
      select Ltrim(value) as [COLLECTION] from T1
    ),T3 as(
      select * from T2 where [COLLECTION] like 'tl:collection:%'
    ),T4 as(
      select trim('tl:collection:' from [COLLECTION]) as [COLLECTION] from T3)
    select row_number()over(order by [COLLECTION]) as Rno,[COLLECTION] from T4;
    
    

    Output:

    User's image

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.