24th Jul 2009
How to get delimiter separated values in the tabular format in SQL
When ever we write script or code in SQL Server we all face problem like following.
“We store the value in database with the delimiter. Now we want to use that value in the another Procedures or Views and may be to display in reports or any where else in the system. At that time we face problem in getting data out of those delimiter”
So lets see how to get the data in the table format from one column value in which values are stored with some delimiter.
Below is the table value function which gives desired output in the table format.
Parameters which need to be passed in function are:
@String = string value which needs to be formatted for e.g. string like ‘IT,Marketing,Production’. Here as we can see values are separated by comma “,”.
@Delimiter = Delimiter like coma(,) , Pipe(|) etc.
Function Code
==========
Create FUNCTION dbo.Split_String(@String nvarchar(4000), @Delimiter char(1))
RETURNS @Results TABLE (Items nvarchar(4000))
AS
BEGIN
DECLARE @No INT
DECLARE @Item nvarchar(4000)
– Initilize the @no variable with 1
SELECT @No = 1
– if null then return
IF @String IS NULL RETURN
WHILE @No !=0
BEGIN
– get the first o
SELECT @No = CHARINDEX(@Delimiter,@String)
– get the value in the @item variable
IF @No !=0
SELECT @Item = LEFT(@STRING,@No – 1)
ELSE
SELECT @Item = @String
– Put the values in the Result Set
INSERT INTO @Results(Items) VALUES(@Item)
– Remove the item from the main string
SELECT @STRING = RIGHT(@String,LEN(@String) – @No)
– break it work done
IF LEN(@String) = 0 BREAK
END
RETURN
END
Lets check Example of it:
==============================
Select * from dbo.Split_String(’IT,Marketing,Production’,',’)
Output in table format:
==============================
IT
Marketing
Production
Great! we have all the data in tabular format now which you can easily use anywhere else in your code.
I hope this little function will help you save lot of time and frustration. Please comment if you know any better solution for this.
When ever we write script or code in SQL Server we all face problem like following.
“We store the value in database with the delimiter. Now we want to use that value in the another Procedures or Views and may be to display in reports or any where else in the system. At that time we face problem in getting data out of those delimiter”
So lets see how to get the data in the table format from one column value in which values are stored with some delimiter.
Below is the table value function which gives desired output in the table format.
Parameters which need to be passed in function are:
@String = string value which needs to be formatted for e.g. string like ‘IT,Marketing,Production’. Here as we can see values are separated by comma “,”.
@Delimiter = Delimiter like coma(,) , Pipe(|) etc.
Function Code
==========
Create FUNCTION dbo.Split_String(@String nvarchar(4000), @Delimiter char(1))
RETURNS @Results TABLE (Items nvarchar(4000))
AS
BEGIN
DECLARE @No INT
DECLARE @Item nvarchar(4000)
– Initilize the @no variable with 1
SELECT @No = 1
– if null then return
IF @String IS NULL RETURN
WHILE @No !=0
BEGIN
– get the first o
SELECT @No = CHARINDEX(@Delimiter,@String)
– get the value in the @item variable
IF @No !=0
SELECT @Item = LEFT(@STRING,@No – 1)
ELSE
SELECT @Item = @String
– Put the values in the Result Set
INSERT INTO @Results(Items) VALUES(@Item)
– Remove the item from the main string
SELECT @STRING = RIGHT(@String,LEN(@String) – @No)
– break it work done
IF LEN(@String) = 0 BREAK
END
RETURN
END
Lets check Example of it:
==============================
Select * from dbo.Split_String(’IT,Marketing,Production’,',’)
Output in table format:
==============================
IT
Marketing
Production
Great! we have all the data in tabular format now which you can easily use anywhere else in your code.
I hope this little function will help you save lot of time and frustration. Please comment if you know any better solution for this.
Posted by kuldip.bhatt under
Code, SQL Server
1 Comment »


















