Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6738457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:22:16+00:00 2026-05-26T11:22:16+00:00

I have to implement SELECT … FROM … WHERE .. IN query in my

  • 0

I have to implement SELECT ... FROM ... WHERE .. IN query in my stored procedure.

Below is the code from my stored procedure

ALTER PROCEDURE [dbo].[SP_GetQuestionSetMultiCat] 
    -- Add the parameters for the stored procedure here
    @PIN varchar(50),
    @CatIds varchar(50),
    @Range int,
    @Que_Type varchar(50)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    declare @qtId as int;
    select @qtId = Que_Type_Id from dbo.QuestionType_Tbl where Que_Type=@Que_Type;


    -- Insert statements for procedure here

        Select Top(@Range) 
            QId,
            Que_Type_Id,
            Que_Level_Id,
            Que_Category_Id, 
            Que,
            Opt1,
            Opt2,
            Opt3,
            Opt4,
            Ans 
        From 
            dbo.Que_Tbl 
        Where 
            (Que_Category_Id in (cast(@CatIds as varchar)))
            and (Que_Type_Id=@qtId) 
            and (Qid not in (Select Que_Id From dbo.UserQuestion_Mapping where PIN=@PIN and Que_typeID=@qtId))


END

Look at the where condition. The Que_Category_Id is int type. What i want to perform is –

Where Que_Category_Id in (1,2,3,4)

The in values i m passing is a string converted from my C# code.

When I am executing this query like –

exec SP_GetQuestionSetMultiCat '666777','4,5,6',5,'Practice'

it is generating an error –

Conversion failed when converting the varchar value ‘{4,5,6}’ to data type int.

Can anybody help me out how to solve this problem.

Thanks for sharing your valuable time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:22:16+00:00Added an answer on May 26, 2026 at 11:22 am

    1)Conversion failed when converting the varchar value '{4,5,6}' to data type int.

    The reason of this error is data type precedence. INT data type has “higher” precedence than VARCHAR data type (16-INT vs. 27-VARCHAR).
    So, SQL Server is trying to convert '{4,5,6}' to INT and not vice versa.

    2) Instead, I would convert @CatIds to XML and then to a table variable (@IDs) using nodes(…) method:

    SET ANSI_WARNINGS ON;
    
    DECLARE @CatIds VARCHAR(50) = '4,5,6';
    
    DECLARE @x XML;
    SET     @x = '<node>' + REPLACE(@CatIds, ',', '</node> <node>') + '</node>';
    DECLARE @IDs TABLE
    (
        ID INT PRIMARY KEY
    );
    INSERT  @IDs(ID)
    SELECT  t.c.value('.', 'INT')
    FROM    @x.nodes('/node') t(c);
    
    --Test    
    SELECT  *
    FROM    @IDs
    

    3) The next step is to rewrite the query using IN (SELECT ID FROM @IDs) instead of in (cast(@CatIds as varchar)):

    SET ANSI_WARNINGS ON;
    
    ALTER PROCEDURE [dbo].[SP_GetQuestionSetMultiCat] 
        -- Add the parameters for the stored procedure here
        @PIN varchar(50),
        @CatIds varchar(50),
        @Range int,
        @Que_Type varchar(50)
    
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        declare @qtId as int;
        select @qtId = Que_Type_Id from dbo.QuestionType_Tbl where Que_Type=@Que_Type;
    
        --Start: New T-SQL code
        DECLARE @x XML;
        SET     @x = '<node>' + REPLACE(@CatIds, ',', '</node> <node>') + '</node>';
        DECLARE @IDs TABLE
        (
            ID INT PRIMARY KEY
        );
        INSERT  @IDs(ID)
        SELECT  t.c.value('.', 'INT')
        FROM    @x.nodes('/node') t(c);
        --End
    
        -- Insert statements for procedure here
    
            Select Top(@Range) 
                QId,
                Que_Type_Id,
                Que_Level_Id,
                Que_Category_Id, 
                Que,
                Opt1,
                Opt2,
                Opt3,
                Opt4,
                Ans 
            From 
                dbo.Que_Tbl 
            Where 
                --The search condition is rewritten using IN(subquery)
                Que_Category_Id in (SELECT ID FROM @IDs)
                and (Que_Type_Id=@qtId) 
                and (Qid not in (Select Que_Id From dbo.UserQuestion_Mapping where PIN=@PIN and Que_typeID=@qtId))
    
    
    END
    

    4) Call stored procedure:

    exec SP_GetQuestionSetMultiCat '666777','4,5,6',5,'Practice'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement cache for SQL queries. Say we have SELECT id,aa,bb FROM
Let's say I have to implement a piece of T-SQL code that must return
I have to implement an algorithm on data which is (for good reasons) stored
I currently have to implement a query on a postgres database using a prepared
Here is my code. You have to kindly look does it suffer from 'same
I have a feature matrix implemented with Silverlight's Grid where users need to select
I have to implement the VinPower application. They offer a Java version, a C
I have to implement MPI system in a cluster. If anyone here has any
I have to implement a middleware system for file sharing, and it has to
I have to implement a IExceptionHandler for the Enteprise Library 4.1. In my particular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.