Introduction:
In this article I am going to explain how to get comma separated values from xml data in sql server using two suitable examples.
In previous articles i explained How to Read or parse xml data and insert into sql table and Concatenate rows values as a comma separated string using for xml path and stuff in sql server and Drop or truncate parent table by dropping all foreign key constraints and CTE Recursive query to get employee manager hierarchy with level and 20 main differences between stored procedures and functions in sql server
Implementation:
Let’s create two examples to understand how easily we can convert xml data to comma
separated string format.
Let's suppose we have
the XML data as string in the following format
DECLARE @EmployeeXml XML = '<ROOT>
<EMPLOYEE Id="2"/>
<EMPLOYEE Id="8"/>
<EMPLOYEE Id="6"/>
<EMPLOYEE Id="3"/>
<EMPLOYEE Id="1"/>
</ROOT>'
Example 1:
Suppose we want to get Employee
Ids in comma separated string format from above xml string.
The query will be as:
SELECT STUFF(
(
SELECT ', ' + col.value('@Id', 'VARCHAR(10)')
FROM @EmployeeXml.nodes('//EMPLOYEE') AS tab(col)
FOR XML PATH('')
), 1, 2, '') AS Result
Query result will be as:
2, 8, 6, 3,
1
Example 2:
Suppose we want to get employee names in comma separated
string format from xml --string. Since we have only
Employee Ids in XML we need to join employee tables through employeeid. So let’s
create employee table using following script
CREATE TABLE
tbEmployee
(
EmployeeId INT IDENTITY(1,1) PRIMARY KEY,
EmployeeName VARCHAR(50),
Age INT
)
Enter some dummy data
in table using following insert query:
INSERT tbEmployee
VALUES
('Salman',25),
('Ranbeer',21),
('Hrithik',22),
('Aamir',28),
('Shahid',24),
('Sidharth',30),
('Varun',29),
('Kabeer',24),
('Raj',23);
Check table data
SELECT *FROM tbEmployee
EmployeeId
|
EmployeeName
|
Age
|
1
|
Salman
|
25
|
2
|
Ranbeer
|
21
|
3
|
Hrithik
|
22
|
4
|
Aamir
|
28
|
5
|
Shahid
|
24
|
6
|
Sidharth
|
30
|
7
|
Varun
|
29
|
8
|
Kabeer
|
24
|
9
|
Raj
|
23
|
Now we need to join data
from xml and employee table to get the desired result.
The query will be as:
SELECT STUFF(
(
SELECT ',' + CAST(EmployeeName AS VARCHAR(100))
FROM
(
SELECT EM1.EmployeeName
FROM tbEmployee0 EM1
INNER JOIN (SELECT col.value('@Id','INT') AS EmployeeId FROM @EmployeeXml.nodes('//EMPLOYEE')tab(col)) EM2
ON EM1.EmployeeId=EM2.EmployeeId
)tb
ORDER BY EmployeeName
FOR XML PATH('')
), 1, 1, '') AS Result
Query result will be
as:
Hrithik,
Kabeer, Ranbeer, Salman, Sidharth
Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..