I have a table data as
col1 col2 col3 col4
11 12 xx yy 20
22 13 qq aa 21
my TDD requried using TSQL as
col1 col2 col3 col4
11 12 xx 20
11 12 yy 20
22 13 qq 21
22 开发者_JS百科13 aa 21
basically, col3 is having values with space, need to create the rows for each value separed by space value.
thanks prav
Have a look at something like this
DECLARE @Table TABLE(
col1 INT,
col2 INT,
col3 VARCHAR(50),
col4 INT
)
INSERT INTO @Table SELECT 11,12,'xx yy',20
INSERT INTO @Table SELECT 22,13,'qq aa',21
;WITH Vals AS (
SELECT *,
CAST('<d>' + REPLACE(col3, ' ', '</d><d>') + '</d>' AS XML) XMLCol
FROM @Table
)
SELECT Vals.*,
T.split.value('.', 'VARCHAR(MAX)') SplitValue
FROM Vals
CROSS APPLY
XMLCol.nodes('/d') T(split)
精彩评论