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. General Programming
  3. C#
  4. How do I pass a wildcard to a data source query

How do I pass a wildcard to a data source query

Scheduled Pinned Locked Moved C#
databasequestioncsharpcom
8 Posts 3 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.
  • D Offline
    D Offline
    Dan Neely
    wrote on last edited by
    #1

    What I want to do is something like this, which fails because NumericID is an int type.

    ds_Search.SelectParameters.Add("NumericID", "%");

    The only relevant hit I was able to find was from the dubious quality website expert exchange (first result, very bottom of the page)[^]which said I either needed to change the DB to have a string field (not possible) or write seperate queries for the wildcards (impractical because there're dozens that would need duplicated). Are those really my only options?

    Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

    J L 2 Replies Last reply
    0
    • D Dan Neely

      What I want to do is something like this, which fails because NumericID is an int type.

      ds_Search.SelectParameters.Add("NumericID", "%");

      The only relevant hit I was able to find was from the dubious quality website expert exchange (first result, very bottom of the page)[^]which said I either needed to change the DB to have a string field (not possible) or write seperate queries for the wildcards (impractical because there're dozens that would need duplicated). Are those really my only options?

      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

      J Offline
      J Offline
      Jason C Bourne
      wrote on last edited by
      #2

      A numeric is not a string, but you can convert the NumericId to a string in a query. Only then can it be compared with a string.

      Jean-Christophe Grégoire

      D 1 Reply Last reply
      0
      • J Jason C Bourne

        A numeric is not a string, but you can convert the NumericId to a string in a query. Only then can it be compared with a string.

        Jean-Christophe Grégoire

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #3

        Do you mean a query that would take a string parameter and convert it into a number before looking at the table? If so, could you give an example?

        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

        1 Reply Last reply
        0
        • D Dan Neely

          What I want to do is something like this, which fails because NumericID is an int type.

          ds_Search.SelectParameters.Add("NumericID", "%");

          The only relevant hit I was able to find was from the dubious quality website expert exchange (first result, very bottom of the page)[^]which said I either needed to change the DB to have a string field (not possible) or write seperate queries for the wildcards (impractical because there're dozens that would need duplicated). Are those really my only options?

          Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

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

          What records are you selecting with that? Every record that has an int-value of "%"? I don't understand what you are trying to do here. What is the SQL-variant of the query you wanna run?

          I are troll :)

          D 1 Reply Last reply
          0
          • L Lost User

            What records are you selecting with that? Every record that has an int-value of "%"? I don't understand what you are trying to do here. What is the SQL-variant of the query you wanna run?

            I are troll :)

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #5

            Hopefully this'll still make sense after being (heavily) cut down and generalized, the real query has more stuff in all 3 sections of sql statement.

            SELECT table1.table1ID AS ID, table2.OrgTypeID
            FROM (table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID)
            WHERE (table2.table4ID = ?)

            table2.table4ID is selected from a listbox that contains all the values in table4 (each giving a table4ID value) or "all" in which case I want to pass a wildcard in.

            Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

            L 1 Reply Last reply
            0
            • D Dan Neely

              Hopefully this'll still make sense after being (heavily) cut down and generalized, the real query has more stuff in all 3 sections of sql statement.

              SELECT table1.table1ID AS ID, table2.OrgTypeID
              FROM (table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID)
              WHERE (table2.table4ID = ?)

              table2.table4ID is selected from a listbox that contains all the values in table4 (each giving a table4ID value) or "all" in which case I want to pass a wildcard in.

              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

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

              dan neely wrote:

              or "all" in which case I want to pass a wildcard in.

              How about removing the

              WHERE (table2.table4ID = ?)

              If you omit the WHERE-clause altogether, wouldn't that effectively give you all the records that a wildcard would select?

              I are troll :)

              D 1 Reply Last reply
              0
              • L Lost User

                dan neely wrote:

                or "all" in which case I want to pass a wildcard in.

                How about removing the

                WHERE (table2.table4ID = ?)

                If you omit the WHERE-clause altogether, wouldn't that effectively give you all the records that a wildcard would select?

                I are troll :)

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #7

                See my original post. Sometimes I need a wildcard, sometimes I have a specific value. I'd prefer not having to duplicate a dozenish queries if I don't have to in order to implement the wildcard because of the increased maintainability difficulty.

                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                L 1 Reply Last reply
                0
                • D Dan Neely

                  See my original post. Sometimes I need a wildcard, sometimes I have a specific value. I'd prefer not having to duplicate a dozenish queries if I don't have to in order to implement the wildcard because of the increased maintainability difficulty.

                  Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

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

                  As far as I know, ANSI-SQL supports wildcards only for filtering on text-based columns, through the LIKE-operator. This option gives you two separate queries;

                  SELECT table1.table1ID AS ID, table2.OrgTypeID
                  FROM (table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID)
                  WHERE (table2.table4ID = 10)

                  (10 being an example of a specific value)

                  SELECT table1.table1ID AS ID, table2.OrgTypeID
                  FROM (table1 INNER JOIN table2 ON table1.table2ID = table2.table2ID)
                  WHERE (CAST(table2.table4ID AS NVARCHAR) LIKE ('%'))

                  The second version will convert all values in the table4ID-column to the NVARCHAR datatype.

                  I are troll :)

                  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