sql query,loop

rdboy_9727 61 Reputation points
2021-11-29T11:00:35.553+00:00

Hi, I have got another query ..

    IF OBJECT_ID('dbo.test', 'U') IS NOT NULL   
      DROP TABLE dbo.test;   
      
    create table test (clmn int , modalities nvarchar(175) )  
    go  
      
    insert into test values (1,'General Dental|General Dental|General Dental|General Dental|General Dental|General Dental|General Dental|General Dental|General Dental|General Dental|General Dental')  
    insert into test values (2,'Chiropractic|Chiropractic|Chiropractic|Chiropractic|Chiropractic')  
       
    go  
      
    select * from dbo.test;  
    go  
      
      
      
    now the i/p can have three rows, four rows etc.. so this table can keep changing  

desired o/p

Blockquote153314-capture.png

Developer technologies Transact-SQL
{count} votes

Accepted answer
  1. Ronen Ariely 15,206 Reputation points
    2021-11-30T02:23:29.197+00:00

    The guessing game continue... Maybe this is what you want?

    SELECT t.clmn,v.ordinal ,v.value as MODALITY  
    FROM dbo.test t   
    CROSS APPLY STRING_SPLIT(t.modalities,'|',1) v  
    GO  
    

    This fits your requested result

    153515-image.png

    Note, I only got this idea after I saw @EchoLiu-MSFT guess that you need t split the content. Using the third parameter can solve the needs in this case (it is a optional parameter which is not supported in current versions of SQL Server on premises)

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-11-30T01:44:16.053+00:00

    Hi @rdboy_9727 ,

    Sorry, your question is not clear enough for me. Maybe this is what you want:

    SELECT t.clmn,v.value as MODALITY  
    FROM dbo.test t   
    CROSS APPLY STRING_SPLIT(t.modalities,'|') v  
    

    Output:
    153512-image.png

    Regards,
    Echo


    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".

    1 person found this answer helpful.
    0 comments No comments

  2. rdboy_9727 61 Reputation points
    2021-11-30T02:38:53.097+00:00

    Hi all, sorry for not being clear on my query . i'll be more specific going ahead .

    @EchoLiu-MSFT , you have 99% given what I want . Incase I need another column in my results showing the count i.e 1,2,3,4,5,6,7,8,9,10,11 for clmn 1 & 1,2,3,4,5 for clmn 2.. I hope it makes sense.

    Regards,
    Dennis

    0 comments No comments

  3. EchoLiu-MSFT 14,621 Reputation points
    2021-11-30T02:44:57.943+00:00

    Please try:

    SELECT ROW_NUMBER() OVER(PARTITION BY t.clmn ORDER BY t.clmn) i,  
    v.value as MODALITY  
    FROM dbo.test t   
    CROSS APPLY STRING_SPLIT(t.modalities,'|') v  
    

    Output:
    153502-image.png

    Regards,
    Echo


    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".

    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.