Click here to Skip to main content
Licence CPOL
First Posted 4 Nov 2008
Views 41,628
Bookmarked 22 times

Split parameter string from comma seperator in SQL IN clause

By | 4 Nov 2008 | Article
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

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.

            
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.

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.

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)

About the Author

Vimal Panara

Technical Lead
IndiaNIC Infotech Ltd
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTry set based approach instead PinmemberSean Lange5:57 7 Oct '11  
GeneralMy vote of 5 PinmemberRicha Sharma from Chandigarh22:12 24 Feb '11  
GeneralMy vote of 5 PinmemberRicha Sharma from Chandigarh20:29 24 Feb '11  
GeneralUse Dynamic SQL Pinmemberrdernst2:55 7 Nov '08  
AnswerRe: Use Dynamic SQL PinmemberJeffSpicolie13:57 1 Apr '09  
The problem with that is the IN statement is incredibly slow, actually horrifically slow with the more values supplied. It is faster to declare a temp table populate it and the use INNER JOIN instead. http://buzzuti.com/post/SQL-WHERE-IN-the-heck-is-the-performance.aspx[^]
GeneralRe: Use Dynamic SQL PinmemberSean Lange5:55 7 Oct '11  
GeneralAnother technique for same problem [without need of any function] Pinmemberkamii471:46 5 Nov '08  
GeneralRe: Another technique for same problem [without need of any function] Pinmemberparybaba21:57 26 Sep '10  
GeneralRe: Another technique for same problem [without need of any function] Pinmemberkamii473:44 27 Sep '10  
GeneralRe: Another technique for same problem [without need of any function] PinmemberEdwin Jose G20:53 28 Mar '11  
QuestionJoin in the example ? Pinmemberhaathi4:59 4 Nov '08  
GeneralCharIndex PinmemberZeXr04:15 4 Nov '08  
QuestionAm I missing something here? PinmemberRichardGrimmer4:03 4 Nov '08  
AnswerRe: Am I missing something here? Pinmemberg_p_l4:34 4 Nov '08  
GeneralRe: Am I missing something here? PinmemberRichardGrimmer4:55 4 Nov '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120604.1 | Last Updated 4 Nov 2008
Article Copyright 2008 by Vimal Panara
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid