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. How to convert varchar values to int while passing as an in parameter?

How to convert varchar values to int while passing as an in parameter?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
9 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.
  • M Offline
    M Offline
    meeram395
    wrote on last edited by
    #1

    I have an sql query which gives the output as follows: OutputFileid ----------- 4 5 6 I want to get this output in horizontal fashion , like 4,5,6 so that I can pass to another query as an 'in' parameter. I was getting that, but the second query returned an error saying that "Conversion failed when converting the varchar value '4,5,6' to data type int". Following is the full stored procedure which i am using for getting the desired result.

    ALTER procedure [dbo].[IsBCastRefExistsforProcess]
    (@processId int)
    as
    begin
    DECLARE @FILEIDs varchar(100)
    select @FILEIDs= COALESCE(@FILEIDs+',' ,'')+ convert(varchar,outputfileid)+''from OutputFiles where ProcessId = @processid
    PRINT @FILEIDs
    if (LEN(@FILEIDs) > 0)
    begin

    select eg.EJVColumnId,eg.EJVColumnName
    from EJVGedaColumns eg,
    GedaColumnMapping gc
    where eg.EJVColumnId = gc.EJVColumnId
    and eg.EJVColumnName like '%BCAST_REF%'
    and gc.OutputFileid in (@FILEIDs)
    end
    end

    when I tried to execute exec IsBCastRefExistsforProcess 1234 it is giving the above error. Is there anyway which i can achieve this?

    select @FILEIDs= COALESCE(@FILEIDs+',' ,'')+ convert(varchar,outputfileid)+''from OutputFiles where ProcessId = @processid

    This gives me the values in horizontal fashion. I tried giving 'and gc.OutputFileid in (Convert(int,@FILEIDs)) - But end up with same error. Please help. Thanks

    Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

    M L 2 Replies Last reply
    0
    • M meeram395

      I have an sql query which gives the output as follows: OutputFileid ----------- 4 5 6 I want to get this output in horizontal fashion , like 4,5,6 so that I can pass to another query as an 'in' parameter. I was getting that, but the second query returned an error saying that "Conversion failed when converting the varchar value '4,5,6' to data type int". Following is the full stored procedure which i am using for getting the desired result.

      ALTER procedure [dbo].[IsBCastRefExistsforProcess]
      (@processId int)
      as
      begin
      DECLARE @FILEIDs varchar(100)
      select @FILEIDs= COALESCE(@FILEIDs+',' ,'')+ convert(varchar,outputfileid)+''from OutputFiles where ProcessId = @processid
      PRINT @FILEIDs
      if (LEN(@FILEIDs) > 0)
      begin

      select eg.EJVColumnId,eg.EJVColumnName
      from EJVGedaColumns eg,
      GedaColumnMapping gc
      where eg.EJVColumnId = gc.EJVColumnId
      and eg.EJVColumnName like '%BCAST_REF%'
      and gc.OutputFileid in (@FILEIDs)
      end
      end

      when I tried to execute exec IsBCastRefExistsforProcess 1234 it is giving the above error. Is there anyway which i can achieve this?

      select @FILEIDs= COALESCE(@FILEIDs+',' ,'')+ convert(varchar,outputfileid)+''from OutputFiles where ProcessId = @processid

      This gives me the values in horizontal fashion. I tried giving 'and gc.OutputFileid in (Convert(int,@FILEIDs)) - But end up with same error. Please help. Thanks

      Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

      M Offline
      M Offline
      meeram395
      wrote on last edited by
      #2

      I fixed it!!. I put the same in a dynamic sql and it got worked. Like this,

      SET @SQL = 'select eg.EJVColumnId,eg.EJVColumnName,gc.OutputFileid from EJVGedaColumns eg,GedaColumnMapping gc
      where eg.EJVColumnId = gc.EJVColumnId and eg.EJVColumnName like ''%BCAST_REF%''
      and gc.OutputFileid in ('+@FILEIDs+')'

      EXEC (@SQL)
      

      Hope this will help somebody. Thanks

      Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

      H 1 Reply Last reply
      0
      • M meeram395

        I have an sql query which gives the output as follows: OutputFileid ----------- 4 5 6 I want to get this output in horizontal fashion , like 4,5,6 so that I can pass to another query as an 'in' parameter. I was getting that, but the second query returned an error saying that "Conversion failed when converting the varchar value '4,5,6' to data type int". Following is the full stored procedure which i am using for getting the desired result.

        ALTER procedure [dbo].[IsBCastRefExistsforProcess]
        (@processId int)
        as
        begin
        DECLARE @FILEIDs varchar(100)
        select @FILEIDs= COALESCE(@FILEIDs+',' ,'')+ convert(varchar,outputfileid)+''from OutputFiles where ProcessId = @processid
        PRINT @FILEIDs
        if (LEN(@FILEIDs) > 0)
        begin

        select eg.EJVColumnId,eg.EJVColumnName
        from EJVGedaColumns eg,
        GedaColumnMapping gc
        where eg.EJVColumnId = gc.EJVColumnId
        and eg.EJVColumnName like '%BCAST_REF%'
        and gc.OutputFileid in (@FILEIDs)
        end
        end

        when I tried to execute exec IsBCastRefExistsforProcess 1234 it is giving the above error. Is there anyway which i can achieve this?

        select @FILEIDs= COALESCE(@FILEIDs+',' ,'')+ convert(varchar,outputfileid)+''from OutputFiles where ProcessId = @processid

        This gives me the values in horizontal fashion. I tried giving 'and gc.OutputFileid in (Convert(int,@FILEIDs)) - But end up with same error. Please help. Thanks

        Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

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

        As an alternative; you could create a function that returns[^] a table (with those ints). You could pass these even as parameters[^] :)

        I are Troll :suss:

        P J 2 Replies Last reply
        0
        • M meeram395

          I fixed it!!. I put the same in a dynamic sql and it got worked. Like this,

          SET @SQL = 'select eg.EJVColumnId,eg.EJVColumnName,gc.OutputFileid from EJVGedaColumns eg,GedaColumnMapping gc
          where eg.EJVColumnId = gc.EJVColumnId and eg.EJVColumnName like ''%BCAST_REF%''
          and gc.OutputFileid in ('+@FILEIDs+')'

          EXEC (@SQL)
          

          Hope this will help somebody. Thanks

          Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

          H Offline
          H Offline
          Hiren solanki
          wrote on last edited by
          #4

          meeram395 wrote:

          EXEC (@SQL)

          other then EXEC you can also use sp_executesql procedure that will give you a way to input parameter with a strong datatype to avoid data mismatch conflict.

          Regards, Hiren. Microsoft Dynamics CRM

          My Recent Article: - Way to know which control have raised PostBack[^]

          M 1 Reply Last reply
          0
          • L Lost User

            As an alternative; you could create a function that returns[^] a table (with those ints). You could pass these even as parameters[^] :)

            I are Troll :suss:

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            10!

            L 1 Reply Last reply
            0
            • L Lost User

              As an alternative; you could create a function that returns[^] a table (with those ints). You could pass these even as parameters[^] :)

              I are Troll :suss:

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #6

              This is one of the times that I'm sad that I work in Oracle. No such thing in Oracle as I know of.

              "When did ignorance become a point of view" - Dilbert

              1 Reply Last reply
              0
              • P PIEBALDconsult

                10!

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

                Thanks :-D

                M 1 Reply Last reply
                0
                • L Lost User

                  Thanks :-D

                  M Offline
                  M Offline
                  meeram395
                  wrote on last edited by
                  #8

                  Thank you very much Eddy. This is too good.

                  Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

                  1 Reply Last reply
                  0
                  • H Hiren solanki

                    meeram395 wrote:

                    EXEC (@SQL)

                    other then EXEC you can also use sp_executesql procedure that will give you a way to input parameter with a strong datatype to avoid data mismatch conflict.

                    Regards, Hiren. Microsoft Dynamics CRM

                    My Recent Article: - Way to know which control have raised PostBack[^]

                    M Offline
                    M Offline
                    meeram395
                    wrote on last edited by
                    #9

                    Thanks very much Hiren, for the help. I will try that.

                    Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

                    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