Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Where .. IN ...

Where .. IN ...

Scheduled Pinned Locked Moved Database
database
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eyalle
    wrote on last edited by
    #1

    Hello all I have this query

    DECLARE @a nvarchar(50)
    SET @a = '1,3'

    select *
    from tableName
    where CAST(ColumnId as nvarchar(50)) in (@a)

    which doesn't work. i would like it to work like the following query

    select *
    from tableName
    where ColumnId in (1,3)

    please advice Thank's

    V M B L 4 Replies Last reply
    0
    • E eyalle

      Hello all I have this query

      DECLARE @a nvarchar(50)
      SET @a = '1,3'

      select *
      from tableName
      where CAST(ColumnId as nvarchar(50)) in (@a)

      which doesn't work. i would like it to work like the following query

      select *
      from tableName
      where ColumnId in (1,3)

      please advice Thank's

      V Offline
      V Offline
      Vipin_Arora
      wrote on last edited by
      #2

      Hi, Try with two diffrenet integer parameters if your columnID is integer. its working fine 4 me..

      Happy Coding... :)

      1 Reply Last reply
      0
      • E eyalle

        Hello all I have this query

        DECLARE @a nvarchar(50)
        SET @a = '1,3'

        select *
        from tableName
        where CAST(ColumnId as nvarchar(50)) in (@a)

        which doesn't work. i would like it to work like the following query

        select *
        from tableName
        where ColumnId in (1,3)

        please advice Thank's

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        You are trying to compare a string '1,3' with an integer field that is not going to work. Either do as Wipin suggested and use 2 variables or change the entire query to dynamic sql

        DECLARE @a nvarchar(50)
        SET @a = 'select * from tableName where ColumnId in (1,3)'
        exec (@a)

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • E eyalle

          Hello all I have this query

          DECLARE @a nvarchar(50)
          SET @a = '1,3'

          select *
          from tableName
          where CAST(ColumnId as nvarchar(50)) in (@a)

          which doesn't work. i would like it to work like the following query

          select *
          from tableName
          where ColumnId in (1,3)

          please advice Thank's

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          You can try this:

          DECLARE @a nvarchar(50)
          SET @a = '1,3'

          declare @tsql as nvarchar(max)
          set @tsql = 'select *
          from tableName
          where CAST(ColumnId as nvarchar(50)) in ('+@a+')'

          exec (@tsql)


          I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

          1 Reply Last reply
          0
          • E eyalle

            Hello all I have this query

            DECLARE @a nvarchar(50)
            SET @a = '1,3'

            select *
            from tableName
            where CAST(ColumnId as nvarchar(50)) in (@a)

            which doesn't work. i would like it to work like the following query

            select *
            from tableName
            where ColumnId in (1,3)

            please advice Thank's

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I'd advice to not use the "in" operator with a user-defined varchar, but to name the values individually.

            SELECT *
            FROM tableName
            WHERE ColumnId = 1 OR ColumnId = 2

            You can simply concatenate "OR ColumnId = @value" to the query and set the parameters.

            Bastard Programmer from Hell :suss:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups