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. Searching through multiple fields

Searching through multiple fields

Scheduled Pinned Locked Moved Database
databasealgorithmshelptutorialquestion
6 Posts 2 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
    monrobot13
    wrote on last edited by
    #1

    I'm wondering if someone could help me with the SQL I'd need to accomplish the following: I have a db with a number of text fields and what I want to do is have a search box in my program that will search all fields for the text entered. For example I have first_name, last_name, alias, etc. and let's say I enter "Jim" in the search field. I want to be able to search all the fields (first_name, last_name, etc) for that term. Is this possible? I'd also like to use the LIKE operator so the text doesn't have to be exact. Thanks!

    - Aaron

    D 1 Reply Last reply
    0
    • M monrobot13

      I'm wondering if someone could help me with the SQL I'd need to accomplish the following: I have a db with a number of text fields and what I want to do is have a search box in my program that will search all fields for the text entered. For example I have first_name, last_name, alias, etc. and let's say I enter "Jim" in the search field. I want to be able to search all the fields (first_name, last_name, etc) for that term. Is this possible? I'd also like to use the LIKE operator so the text doesn't have to be exact. Thanks!

      - Aaron

      D Offline
      D Offline
      Dinuj Nath
      wrote on last edited by
      #2

      Maybe something like this: Select * from table where first_name like '%searchString%' or last_name like '%searchString%' or .... Or Select * from table where first_name + last_name + .... like '%searchString%'

      -- Don't take life seriously because you can't come out of it alive. -Warren Miller

      M 1 Reply Last reply
      0
      • D Dinuj Nath

        Maybe something like this: Select * from table where first_name like '%searchString%' or last_name like '%searchString%' or .... Or Select * from table where first_name + last_name + .... like '%searchString%'

        -- Don't take life seriously because you can't come out of it alive. -Warren Miller

        M Offline
        M Offline
        monrobot13
        wrote on last edited by
        #3

        That seems to work quite well. Wondering if you could help with another question...how would I make the search string dynamic, as in I want to be able to pass in the value as a paramter...can I do this, if so what's the syntax? Thanks very much for the help.

        - Aaron

        D 1 Reply Last reply
        0
        • M monrobot13

          That seems to work quite well. Wondering if you could help with another question...how would I make the search string dynamic, as in I want to be able to pass in the value as a paramter...can I do this, if so what's the syntax? Thanks very much for the help.

          - Aaron

          D Offline
          D Offline
          Dinuj Nath
          wrote on last edited by
          #4

          I did not get it. Could you explain a little more about what you want to do?

          -- Don't take life seriously because you can't come out of it alive. -Warren Miller

          M 1 Reply Last reply
          0
          • D Dinuj Nath

            I did not get it. Could you explain a little more about what you want to do?

            -- Don't take life seriously because you can't come out of it alive. -Warren Miller

            M Offline
            M Offline
            monrobot13
            wrote on last edited by
            #5

            Sure. This is basically what my T-SQL looks like:

            SELECT * FROM dbo.Books
            WHERE title LIKE '%search%' OR author LIKE '%search%'

            What I want to do is be able to pass in "search" as a parameter. I'm not sure how to do this though because if I remove the quotes around the search sting and add the @ in front of it, I get a syntax error. I hope that's clearer. Thanks again for the help.

            - Aaron

            D 1 Reply Last reply
            0
            • M monrobot13

              Sure. This is basically what my T-SQL looks like:

              SELECT * FROM dbo.Books
              WHERE title LIKE '%search%' OR author LIKE '%search%'

              What I want to do is be able to pass in "search" as a parameter. I'm not sure how to do this though because if I remove the quotes around the search sting and add the @ in front of it, I get a syntax error. I hope that's clearer. Thanks again for the help.

              - Aaron

              D Offline
              D Offline
              Dinuj Nath
              wrote on last edited by
              #6

              Maybe something like this: Declare @a varchar(50) SELECT * FROM dbo.Books WHERE title LIKE '%' + @a + '%' OR author LIKE '%' + @a + '%'

              -- Don't take life seriously because you can't come out of it alive. -Warren Miller

              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