Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hello friends,
I have Table name as country like.
SQL
Id    CountryName
1     India
2     Aus
3     Pak
4     US
5     Eng


And my stored procedure is like.
SQL
CREATE PROCEDURE getCountry
@Id(VARCHAR 10)
AS
BEGIN
      IF @Id IS NOT NULL
         SELECT CountryName FROM Country where Id = @Id
      END
END
GO


I am sending the parameter as Id = 1,2,3,4,5 in string format,
How can we extract the comma separated?
Posted
Updated 17-Jan-11 19:15pm
v2
Comments
TweakBird 18-Jan-11 1:18am    
Edited for code blocks & Capitalization

hi just execute the query in sql server..this will give you an idea about comma seperation.
declare  @roleIds varchar(250),
    @userId int
BEGIN
 set @roleIds='1,2,3,4,5,6,7,'
DECLARE @sItem VARCHAR(8000)
While (Charindex(',',@roleIds)>0)
    Begin
        Select  @sItem = ltrim(rtrim(Substring(@roleIds,1,Charindex(',',@roleIds)-1)))
        Set @roleIds = Substring(@roleIds,Charindex(',',@roleIds)+1,len(@roleIds))
        declare @n int
        SET @n = CONVERT(int, @sItem)
        print @n
    End
END




I hope this may help you....
 
Share this answer
 
v2
CREATE A FUNCTION AS FOLLOWS

SQL
CREATE FUNCTION fn_Split_Up_Ids
(
   @Param_Ids varchar(500)
)
RETURNS @Id_Table TABLE(IDField int)

AS
BEGIN  
   IF (LEN(@Param_Ids) <= 0) 
      RETURN

   DECLARE @CommaPos smallint
   SET @CommaPos = CHARINDEX(',', RTRIM(LTRIM(@Param_Ids)))	  

   IF @CommaPos = 0
       INSERT INTO @Id_Table 
              VALUES(CONVERT(BIGINT ,RTRIM(LTRIM(@Param_Ids))))
   ELSE 
       BEGIN
           WHILE LEN(@Param_Ids) > 1
	   BEGIN
	     SET @CommaPos = CHARINDEX(',', RTRIM(LTRIM(@Param_Ids)))
             INSERT INTO @Id_Table 
                      VALUES(CONVERT(INT ,SUBSTRING(RTRIM(LTRIM(@Param_Ids)),1, @CommaPos - 1)))
	     SET @Param_Ids = SUBSTRING(RTRIM(LTRIM(@Param_Ids)), @CommaPos + 1 , LEN(RTRIM(LTRIM(@Param_Ids))))
	     SET @CommaPos = CHARINDEX(',', RTRIM(LTRIM(@Param_Ids)))
	     IF @CommaPos = 0
	     BEGIN
                 INSERT INTO @Id_Table VALUES(CONVERT(INT ,RTRIM(LTRIM(@Param_Ids))))
                 BREAK
	     END
	   END
       END
       RETURN 
  END



AND THEN YOU NEED TO MODIFY YOUR SP AS BELOW:

SQL
CREATE PROCEDURE spGetCountry
@Id VARCHAR(100)
AS
BEGIN
         SELECT Country FROM tblCountry where Id IN (select * from fn_Split_Up_Ids(@Id))
END
GO


HERE YOU GO :)
NOW CALL YOUR SP
SQL
EXEC spGetCountry '1,2'
 
Share this answer
 
Comments
karthi34 27-Jan-11 9:37am    
Thanks For the Post. Here i have to go for LIKE instead of IN. I tried with that but results negative. (ex) EXEC spGetCountry '1,2'
select * from tblCountry where Id Like(select * from fn_Split_Up_Ids(1 AND 2))

can you help me
Darpanw 27-Jan-11 9:58am    
Hi Karthi,

First thing is -
fn_Split_Up_Ids will accept parameter as comma separated value (i.e. '1,2,3,4' etc....) so fn_Split_Up_Ids(1 AND 2) will not work.

and second thing is that-
fn_Split_Up_Ids returns a table, so you cannot use like in this example.
Like is used for pattern matching in a string but fn_Split_Up_Ids return a table data not a single string.

If you can give detailed description of your problem so i will try to help you out :)

Regards
Darpan
There is no direct function to split a string in sql query. You need to make one of your own. Below is an example:

http://geekswithblogs.net/AngelEyes/archive/2007/04/12/111504.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900