User Id :    Password :      New Member   Forgot Password
 
Sql Server function to convert comma separated value into tabular format
Description Some times when writing sql query we have comma separated values and we want to convert it into tabular format so that we can apply in clause easily. So for that purpose I am giving a function that can perform this task very easily. You need to pass only the comma separated values as argument and it return the tabular format.   No. of Views     2080
  Rating     5
Author Sumit Gupta   Posted On     11 Jul 2011
Tags SQL SERVER,General    

Sample Code   Download Code

Create Function fn_GetSeprateValues(@Ids varchar(8000))
returns @Table Table(Id varchar(100))
As
Begin

Declare @ind int
Declare @Start int

Set @Ids=@Ids+','
Set @Start=1

Set @ind =charindex(',',@Ids)
    While(@ind>0)
    Begin
        Declare @SubId varchar(100)
        Set @SubId=Substring(@Ids,@Start,(@ind-1))
        if(@SubId<>'' and @SubId Is Not Null)
        Begin
            insert into @Table(Id) values(@SubId)
       End
        Set @Ids =Substring(@Ids,@ind+1,Len(@Ids))
        SET @ind = charindex(',',@Ids ) 
    End

Return

End

 

I hope it make sense..

About Author

About Author I am cool and fun loving guy and enjoy my work. I love music and play table-tennis.... Sumit Gupta
No Photo
 
Country India
Company BrickRed Technologies Pvt. Ltd.
Home Page http://www.facebook.com/sumitgupta1225

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Annonymous on 02 Sep 2011 at 10:34 PM
 
Hey, good to find someone who aegers with me. GMTA.
 
 
     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by