Results 1 to 5 of 5

Thread: SQL IN Statement Only Accepts Integer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    SQL IN Statement Only Accepts Integer

    Hi All

    I have the following Stored Procedure that asks for parameter @CaseID in my table that is type INT but how can I insert the values in the parameter when I MUST use string as the variable as the IN statement it only accepts a "," (comma) to sperate the values.

    for example

    Code:
    CREATEPROCEDURE [dbo].[spEvents]
    
    	@CaseID nvarchar(10)
    
    AS
    
    SELECT     tblEvents.CaseID
    FROM       tblEvents
    WHERE     (tblEvents.CaseID IN (@CaseID))
    But using the IN statement it accepts multiple values but it needs to be seperated by a comma "," therefor this becomes a string value. The below is the proper syntax for the IN statement

    Code:
    SELECT column_name(s)
    FROM table_name
    WHERE column_name IN (value1,value2,...)
    so when I goto execute my stored procedure like

    Code:
    exec spEvents @CaseID = '21474,21475'
    It comes back with the following error

    Code:
    Msg 245, Level 16, State 1, Procedure spEvents, Line 7
    Conversion failed when converting the nvarchar value '21474,2147' to data type int.
    Any ideas on how I can overcome this problem?

    So if I use the statement as follows it works fine without the parameter

    Code:
    SELECT     tblEvents.CaseID
    FROM       tblEvents
    WHERE     (tblEvents.CaseID IN (21474,21475))

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL IN Statement Only Accepts Integer

    Quote Originally Posted by Shamas21 View Post
    But using the IN statement it accepts multiple values but it needs to be seperated by a comma "," therefor this becomes a string value.
    No it does not, it stays as multiple values separated by commas.

    (value1,value2,...) is not the same thing as ('value1,value2,...')


    In order to use a single parameter for a list of values you will need to split them out somehow (such as shown in the CodeBank threads Using Parameters with an SQL IN Clause and SQL Server 2005 - Loop through/split a delimited string), or do away with the benefits of a stored procedure and within the SP build+execute a string containing the query and values.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: SQL IN Statement Only Accepts Integer

    Quote Originally Posted by si_the_geek View Post
    No it does not, it stays as multiple values separated by commas.

    (value1,value2,...) is not the same thing as ('value1,value2,...')


    In order to use a single parameter for a list of values you will need to split them out somehow (such as shown in the CodeBank threads Using Parameters with an SQL IN Clause and SQL Server 2005 - Loop through/split a delimited string), or do away with the benefits of a stored procedure and within the SP build+execute a string containing the query and values.
    Many Thanks. I decided to build the SQL statement in my VB project instead by using the

    Code:
                cmd.CommandType = CommandType.Text
                cmd.CommandText = finalSQL

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL IN Statement Only Accepts Integer

    Another option would have been to use the CharIndex function.

    WHERE CharIndex( Cast(CaseId as varchar) + ',' , @CaseId) > 0

    The + ',' is required to ensure an exact match. Otherwise searching for Case Ids 1,77, would return Id 7 as well. Also note the last character in @CaseId must be a comma.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: SQL IN Statement Only Accepts Integer

    Quote Originally Posted by brucevde View Post
    Another option would have been to use the CharIndex function.

    WHERE CharIndex( Cast(CaseId as varchar) + ',' , @CaseId) > 0

    The + ',' is required to ensure an exact match. Otherwise searching for Case Ids 1,77, would return Id 7 as well. Also note the last character in @CaseId must be a comma.
    Hi Bruce

    I need your help. Can you alter the following statement for me to accept mutiple values by using you CharIndex method? Any help is appreciated. Thanks

    Code:
    ALTER PROCEDURE [dbo].[spReportsInvoicedByConsultant]
    
    	@FromDate datetime,
    	@ToDate datetime,
    	@ConsultantName nvarchar(MAX)
    
    AS
    
    SELECT     tblCases.Consultant, SUM(tblInvoicedCharges.Net) AS SumOfNet
    FROM         tblCases INNER JOIN
                          tblInvoicedCharges ON tblCases.CaseID = tblInvoicedCharges.CaseID
    WHERE     (tblInvoicedCharges.Date >= @FromDate) AND (tblInvoicedCharges.Date <= @ToDate) AND (tblCases.Consultant IN (@ConsultantName))
    GROUP BY tblCases.Consultant
    ORDER BY SumOfNet DESC

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width