How to Split aafter comma and display in new Row
i have empid as 1,2,5,3,4
and i want to split and display as
1
2
5
3
4
with emnpname
1=a
2=b
3=c
.........................
please help
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Oct 8, 2013, 4:29 AM
hi,
if you are SQL server 2008 and above user, split function is inbuilt.
http://technet.microsoft.com/en-us/library/aa496058%28v=sql.80%29.aspx
you can also modify this function as per your requirement.
Jaganathan BantheswaranPosted Oct 8, 2013, 4:37 AM
Puneet GuptaPosted Oct 8, 2013, 4:27 AM
http://www.webtechminer.com/split-function-in-sql-server-to-break-comma-separated-string/
Thanks,
Puneet Gupta
Khargesh RajputPosted Oct 8, 2013, 1:43 AM
ALTER FUNCTION [dbo].[Split]
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
but how to use with query in join