Created
March 19, 2014 23:26
-
-
Save MikeMKH/9653803 to your computer and use it in GitHub Desktop.
Shows how to extract multiple values from a column in T-SQL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;WITH cte(value, id) AS ( | |
SELECT * | |
FROM ( | |
VALUES | |
('HERP DREP CHS XMS', 1) | |
,('HELLO WORLD', 2) | |
) AS x(a, b) | |
) | |
SELECT | |
flags.flag.value('.', 'varchar(50)') AS Flag | |
,id AS "Order ID" | |
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS "KEY" | |
FROM ( | |
SELECT | |
CAST('<flag>' + REPLACE(value, ' ', '</flag><flag>') + '</flag>' AS XML) | |
,id | |
FROM cte | |
) AS f(x, id) | |
CROSS APPLY f.x.nodes('flag') AS flags(flag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my blog post for more details: http://comp-phil.blogspot.com/2013/11/working-with-multiple-values-in-column.html