Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this one -</p> <p><strong>Solution #1:</strong></p> <pre><code>DECLARE @t VARCHAR(100) SELECT @t = 'A:B:C;J:K;P:L:J;' SELECT * FROM ( SELECT token = t.c.value('.', 'VARCHAR(100)') FROM ( SELECT xmls = CAST('&lt;t&gt;' + REPLACE( REPLACE(@t, ':', ' '), ';', '&lt;/t&gt;&lt;t&gt;') + '&lt;/t&gt;' AS XML) ) r CROSS APPLY xmls.nodes('/t') AS t(c) ) t WHERE t.token != '' </code></pre> <p><strong>Output:</strong></p> <pre><code>---------- A B C J K P L J </code></pre> <p><strong>Solution #2:</strong></p> <pre><code>DECLARE @t VARCHAR(100) SELECT @t = 'A:B:C;J:K;P:L:J;' PRINT REPLACE(REPLACE(@t, ':', ' '), ';', CHAR(13) + CHAR(13)) </code></pre> <p><strong>Output:</strong></p> <pre><code>A B C J K P L J </code></pre> <p><strong>Solution #3:</strong></p> <pre><code>DECLARE @t VARCHAR(100) SELECT @t = 'A:B:C;J:K;P:L:J;' SELECT [1], [2], [3] FROM ( SELECT t2.id , t2.name , rn2 = ROW_NUMBER() OVER (PARTITION BY t2.id ORDER BY 1/0) FROM ( SELECT id = t.c.value('@n', 'INT') , name = t.c.value('@s', 'CHAR(1)') FROM ( SELECT x = CAST('&lt;t s = "' + REPLACE(token + ':', ':', '" n = "' + CAST(rn AS VARCHAR(10)) + '" /&gt;&lt;t s = "') + '" /&gt;' AS XML) FROM ( SELECT token = t.c.value('.', 'VARCHAR(100)') , rn = ROW_NUMBER() OVER (ORDER BY 1/0) FROM ( SELECT x = CAST('&lt;t&gt;' + REPLACE(@t, ';', '&lt;/t&gt;&lt;t&gt;') + '&lt;/t&gt;' AS XML) ) r CROSS APPLY x.nodes('/t') t(c) ) t ) d CROSS APPLY x.nodes('/t') t(c) ) t2 WHERE t2.name != '' ) t3 PIVOT ( MAX(name) FOR rn2 IN ([1], [2], [3]) ) p </code></pre> <p><strong>Output:</strong></p> <pre><code>1 2 3 ---- ---- ---- A B C J K NULL P L J </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload