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. Strange SQL Query doesn't accept NULL

Strange SQL Query doesn't accept NULL

Scheduled Pinned Locked Moved Database
databasealgorithmshelptutorialquestion
4 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.
  • V Offline
    V Offline
    Vector7
    wrote on last edited by
    #1

    I am using the like statement in searching forms. It works as long as I don't have a column in the SQL DB which haven't NULL as entry. For example : A car and it's color Porsche Grey Honda NULL BMW Black SELECT .... WHERE TYP LIKE 'Porsche%' AND COLOR LIKE '%' works ! Whole Porsche line SELECT .... WHERE TYP LIKE '%' AND COLOR LIKE 'Black' works too. Whole BMWline but both ... SELECT .... WHERE TYP LIKE 'Honda%' AND COLOR LIKE '%' SELECT .... WHERE TYP LIKE 'Honda%' don't work because Column Color for Honda has a NULL entry. Confusing, isn't it ? Is this a bug, or something what can be solved ?

    S 1 Reply Last reply
    0
    • V Vector7

      I am using the like statement in searching forms. It works as long as I don't have a column in the SQL DB which haven't NULL as entry. For example : A car and it's color Porsche Grey Honda NULL BMW Black SELECT .... WHERE TYP LIKE 'Porsche%' AND COLOR LIKE '%' works ! Whole Porsche line SELECT .... WHERE TYP LIKE '%' AND COLOR LIKE 'Black' works too. Whole BMWline but both ... SELECT .... WHERE TYP LIKE 'Honda%' AND COLOR LIKE '%' SELECT .... WHERE TYP LIKE 'Honda%' don't work because Column Color for Honda has a NULL entry. Confusing, isn't it ? Is this a bug, or something what can be solved ?

      S Offline
      S Offline
      Steve S
      wrote on last edited by
      #2

      No, not a bug; it can be confusing though. NULL means "I don't know". You're asking for any colour, but SQL is saying it can't tell if the Honda has a colour (hey, a transparent car!!). You could try: SELECT ... WHERE TYP LIKE 'Honda%' AND (COLOR LIKE '%' OR COLOR IS NULL) I don't know why you're not getting anything for the case where the TYP is specified but not the colour, since that shouldn't cause a problem. Are you doing a single table query, or is it more complex? If the latter, you might not have results for a different reason, like there being no suitable data (no data in one of the other tables to join, for instance) Steve S Developer for hire

      V 1 Reply Last reply
      0
      • S Steve S

        No, not a bug; it can be confusing though. NULL means "I don't know". You're asking for any colour, but SQL is saying it can't tell if the Honda has a colour (hey, a transparent car!!). You could try: SELECT ... WHERE TYP LIKE 'Honda%' AND (COLOR LIKE '%' OR COLOR IS NULL) I don't know why you're not getting anything for the case where the TYP is specified but not the colour, since that shouldn't cause a problem. Are you doing a single table query, or is it more complex? If the latter, you might not have results for a different reason, like there being no suitable data (no data in one of the other tables to join, for instance) Steve S Developer for hire

        V Offline
        V Offline
        Vector7
        wrote on last edited by
        #3

        Hi, I don't know why you're not getting anything for the case where the TYP is specified but not the colour, since that shouldn't cause a problem. Because there are cases in life where you have to accept nulls. Beside your response I have found a further solution. Instead TYP I have to write CARS.TYP and CARS.COLOR CARS is the Table ... and finally it works with the table prefix ;) Unbelievable ...

        S 1 Reply Last reply
        0
        • V Vector7

          Hi, I don't know why you're not getting anything for the case where the TYP is specified but not the colour, since that shouldn't cause a problem. Because there are cases in life where you have to accept nulls. Beside your response I have found a further solution. Instead TYP I have to write CARS.TYP and CARS.COLOR CARS is the Table ... and finally it works with the table prefix ;) Unbelievable ...

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          Vector7 wrote: Because there are cases in life where you have to accept nulls. Yes, I know that; I've been using RDBMS for over 20 years :) What I was trying to point out was that if you perform a syntactically correct select statement SELECT * FROM CARS WHERE TYP = 'Honda%' it should return rows, regardless of whether any column in the table has a NULL value (apart from TYP, obviously!). (Your original post said it wasn't working). If that doesn't work, it suggests issues other than NULL handling. Since you 'fixed' it by specifying a table prefix, that suggests the parser is otherwise being slightly confused by the names. What version and SP of SQL Server are you running? Steve S Developer for hire

          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