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. SQL Query - AND / OR / IF / ELSE

SQL Query - AND / OR / IF / ELSE

Scheduled Pinned Locked Moved Database
databasetutorial
3 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.
  • J Offline
    J Offline
    Joe Stansfield
    wrote on last edited by
    #1

    Hey all, I'm writing a basic parametrized query to search a database and am attempting to allow a user to specify 1 or 2 parameters for a search. However, I am having trouble when the user specifies both parameters as the return of the query then displays 0 rows. Here is what I have: WHERE ((suburb LIKE '%' + @suburb1 + '%' AND @suburb2 ='') OR (suburb LIKE '%' + @suburb1 + '%' AND suburb LIKE '%' + @suburb2 + '%')) @suburb1 and @suburb2 are declared within that statement as parameters for the query (I am using VS2008 to construct it). It looks like I need an IF statement or similar to make it do the first part if @suburb2 = '', however I can not for the life of me work out how to do this... I am relatively new to writing queries, so I understand that I may be way off with how I'm approaching this, but some guidance in the right direction would be greatly appreciated! Joe

    M B 2 Replies Last reply
    0
    • J Joe Stansfield

      Hey all, I'm writing a basic parametrized query to search a database and am attempting to allow a user to specify 1 or 2 parameters for a search. However, I am having trouble when the user specifies both parameters as the return of the query then displays 0 rows. Here is what I have: WHERE ((suburb LIKE '%' + @suburb1 + '%' AND @suburb2 ='') OR (suburb LIKE '%' + @suburb1 + '%' AND suburb LIKE '%' + @suburb2 + '%')) @suburb1 and @suburb2 are declared within that statement as parameters for the query (I am using VS2008 to construct it). It looks like I need an IF statement or similar to make it do the first part if @suburb2 = '', however I can not for the life of me work out how to do this... I am relatively new to writing queries, so I understand that I may be way off with how I'm approaching this, but some guidance in the right direction would be greatly appreciated! Joe

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

      Do some research into parameterised queries[^], this will allow you much greater flexibility and control and reduce the risk of sql injection attacks.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • J Joe Stansfield

        Hey all, I'm writing a basic parametrized query to search a database and am attempting to allow a user to specify 1 or 2 parameters for a search. However, I am having trouble when the user specifies both parameters as the return of the query then displays 0 rows. Here is what I have: WHERE ((suburb LIKE '%' + @suburb1 + '%' AND @suburb2 ='') OR (suburb LIKE '%' + @suburb1 + '%' AND suburb LIKE '%' + @suburb2 + '%')) @suburb1 and @suburb2 are declared within that statement as parameters for the query (I am using VS2008 to construct it). It looks like I need an IF statement or similar to make it do the first part if @suburb2 = '', however I can not for the life of me work out how to do this... I am relatively new to writing queries, so I understand that I may be way off with how I'm approaching this, but some guidance in the right direction would be greatly appreciated! Joe

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Just look at:

        suburb LIKE '%' + @suburb1 + '%' AND suburb LIKE '%' + @suburb2 + '%'

        If @suburb2 is different from @suburb1, that will select exactly 0 rows because suburb is either like @suburb1 or like @suburb2 but surely not like both at the same time. Use OR instead of AND here. To differentiate between @suburb2 being set or not, use a CASE statement.

        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