Click here to Skip to main content
15,893,368 members
Articles / Web Development / XHTML
Article

Split parameter string from comma seperator in SQL IN clause

Rate me:
Please Sign up or sign in to vote.
3.47/5 (12 votes)
4 Nov 2008CPOL 111.9K   24   18
Many time we face problem in SQL Query when performing operation with IN clause to check values seperated by ,(commas) in a parameter.

Introduction

Many time we faced a problem in SQL Query when performing operation with IN clause to check values seperated by commas in a parameter.

like the following

SQL
SELECT * FROM TblJobs WHERE iCategoryID IN (’1,2,3,4,5′)

it gives error like

Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to int.

To overcome this problem here I have written one function that will resolve this issue.

Function Creation:

First create this function in your database.

SQL
IF EXISTS(SELECT * FROM sysobjects WHERE ID = OBJECT_ID(’UF_CSVToTable’))
 DROP FUNCTION UF_CSVToTable
GO

CREATE FUNCTION UF_CSVToTable
(
 @psCSString VARCHAR(8000)
)
RETURNS @otTemp TABLE(sID VARCHAR(20))
AS
BEGIN
 DECLARE @sTemp VARCHAR(10)

 WHILE LEN(@psCSString) > 0
 BEGIN
  SET @sTemp = LEFT(@psCSString, ISNULL(NULLIF(CHARINDEX(',', @psCSString) - 1, -1),
                    LEN(@psCSString)))
  SET @psCSString = SUBSTRING(@psCSString,ISNULL(NULLIF(CHARINDEX(',', @psCSString), 0),
                               LEN(@psCSString)) + 1, LEN(@psCSString))
  INSERT INTO @otTemp VALUES (@sTemp)
 END

RETURN
END
Go

Pass a string with comma seperated values in this function. Function will return table with one column and multiple rows as record separated with string value.

Now how to use this function:

Now implement this function into SQL Query or Procedure.

SQL
CREATE PROCEDURE TEMP_SP_RETRIVE_JOBS
 @sCategoryID VARCHAR(5000)
AS
BEGIN
 SELECT *
 FROM
  TblJobs
 WHERE
  iCategoryID IN (SELECT * FROM UF_CSVToTable(@sCategoryID))
END
GO

Parameter @sCategoryID has values like '1,2,3,4,55,159,86,95'. So this string we are passing into the function as a parameter. And this function will return this value as a table and SQL Server event process will check each value with IN clause.

We have used this function in IN Clause by passing parameter @sCategoryID as string variable with value as string value separated by comma sign(,). See the following query.

SQL
SELECT * FROM tblJobs WHERE iCategoryID IN (
   select * from UF_CSVToTable(’1,2,3,4,5,6,7,15,55,59,86,95′)
)

License

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


Written By
Technical Lead IndiaNIC Infotech Ltd
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSimple Solution Pin
Member 1378688318-Apr-18 15:20
Member 1378688318-Apr-18 15:20 
QuestionThanks.. this is very very usefull Pin
Member 418365313-Jun-13 9:50
Member 418365313-Jun-13 9:50 
Questioncan i write procedure without using function Pin
lavanyaeppala24-Nov-12 6:49
lavanyaeppala24-Nov-12 6:49 
QuestionTry set based approach instead Pin
Sean Lange7-Oct-11 5:57
Sean Lange7-Oct-11 5:57 
GeneralMy vote of 5 Pin
Sharma Richa24-Feb-11 22:12
Sharma Richa24-Feb-11 22:12 
GeneralMy vote of 5 Pin
Sharma Richa24-Feb-11 20:29
Sharma Richa24-Feb-11 20:29 
Thanx,
It really helped me.
GeneralUse Dynamic SQL Pin
rdernst7-Nov-08 2:55
rdernst7-Nov-08 2:55 
AnswerRe: Use Dynamic SQL Pin
JeffSpicolie1-Apr-09 13:57
JeffSpicolie1-Apr-09 13:57 
GeneralRe: Use Dynamic SQL Pin
Sean Lange7-Oct-11 5:55
Sean Lange7-Oct-11 5:55 
GeneralAnother technique for same problem [without need of any function] Pin
KamranShahid5-Nov-08 1:46
KamranShahid5-Nov-08 1:46 
GeneralRe: Another technique for same problem [without need of any function] Pin
parybaba26-Sep-10 21:57
parybaba26-Sep-10 21:57 
GeneralRe: Another technique for same problem [without need of any function] Pin
KamranShahid27-Sep-10 3:44
KamranShahid27-Sep-10 3:44 
GeneralRe: Another technique for same problem [without need of any function] Pin
Edwin Jose G28-Mar-11 20:53
Edwin Jose G28-Mar-11 20:53 
QuestionJoin in the example ? Pin
haathi4-Nov-08 4:59
haathi4-Nov-08 4:59 
GeneralCharIndex Pin
ZeXr04-Nov-08 4:15
ZeXr04-Nov-08 4:15 
QuestionAm I missing something here? Pin
RichardGrimmer4-Nov-08 4:03
RichardGrimmer4-Nov-08 4:03 
AnswerRe: Am I missing something here? Pin
g_p_l4-Nov-08 4:34
g_p_l4-Nov-08 4:34 
GeneralRe: Am I missing something here? Pin
RichardGrimmer4-Nov-08 4:55
RichardGrimmer4-Nov-08 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.