First you need to create a table valued function like as follows
CREATE FUNCTION dbo.SplitString
(
@stringToSplit VARCHAR(MAX)
)
RETURNS @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN
DECLARE @name NVARCHAR(255)
DECLARE @pos INT
WHILE CHARINDEX('|', @stringToSplit) > 0
BEGIN
SELECT @pos = CHARINDEX('|', @stringToSplit)
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos -1)
INSERT INTO @returnList
SELECT @name
SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos + 1, LEN(@stringToSplit) -@pos)
END
INSERT INTO @returnList
SELECT @stringToSplit
RETURN
END
you can test this method as follows
select * from dbo.SplitString('1,sam,IT,Engineer,10000|2,john,IT,Lead,20000|3,abraham,IT,Engineer,10000');
it will return:
1,sam,IT,Engineer,10000
2,john,IT,Lead,20000
3,abraham,IT,Engineer,10000
You can not use this method as like
select * from [dbo].[SplitString]((select data from temporary1));
but it will works
select * from [dbo].[SplitString]((select top 1 data from temporary1));
But why?
If you look carefully the method is accept a single string value. So if you need to use this with any table then you should create a procedure which itereate row by row in your table and pick row and send it to the function. Then function will split string into multiple rows. You just concate/insert/union all rows and use that.
Solution:
you can use cross apply.
select * from temporary1
cross apply [dbo].[SplitString](data);