Splitting comma separated values into row values in SQL
Today we are going to see how to separate the comma separated values in sql. We are often come across these kind of situation to split strings with comma. So i hope this would be more helpful.
--Declaration
DECLARE @xml XML,@commaValues NVARCHAR(2000)
--Initialization
SET @xml = N'<t>' + REPLACE(@commaValues ,',','</t><t>') + '</t>'
--Temporary table creation
CREATE TABLE #tmp(studentid int)
--Inserting values in temporary table
INSERT INTO #tmp
SELECT r.value('.','Nvarchar(MAX)') as item
FROM @xml.nodes('/t') as records(r)
--Displaying the values from the temporary table
SELECT * FROM #tmp
--Declaration
DECLARE @xml XML,@commaValues NVARCHAR(2000)
--Initialization
SET @xml = N'<t>' + REPLACE(@commaValues ,',','</t><t>') + '</t>'
--Temporary table creation
CREATE TABLE #tmp(studentid int)
--Inserting values in temporary table
INSERT INTO #tmp
SELECT r.value('.','Nvarchar(MAX)') as item
FROM @xml.nodes('/t') as records(r)
--Displaying the values from the temporary table
SELECT * FROM #tmp
Comments
Post a Comment