I want to show values from 6 rows for a particular column in single row with 6 columns for 6 values..
Actually we have a column (TestValues) where we are storing 6 values split by '##@##'. But we want it to be shown in different textboxes on UI, but we have to split them in SQL Server stored procedure only.
Current Output using this query:
SELECT Code,Reason,
LTRIM(RTRIM(m.n.value('.[1]','varchar(500)'))) AS TestValues
FROM
(
SELECT Code,Reason, CAST('<XMLRoot><RowData>' + REPLACE(TestValues,'##@##','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM dbo.Information where Logged_ID = 1001
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)
Desired Output:
I have tried below query but it's not working ... Please let me know where I am wrong.
select * from
(
select *, rank() over (partition by TestValues order by code) rank from
(
SELECT Code,Reason,
LTRIM(RTRIM(m.n.value('.[1]','varchar(500)'))) AS TestValues
FROM
(
SELECT Code,Reason, CAST('<XMLRoot><RowData>' + REPLACE(TestValues,'##@##','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM dbo.Information where Logged_ID = 1001
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)
) r
)tab1
pivot
(
max([TestValues]) for rank in ([1],[2],[3],[4],[5],[6])) pv



TestValuesis12345,23456,'',123456,1234567,12345? I can't think of haveORDER BYclause that would be able to create that order (especially as12345is both at the end and start). Also, please don't post data as an image, provide it astext. You have enough reputation to know that. 😊ORDER BYclause? With the data in that image it would be impossible, thus which column a value would be assigned to would be completely random.