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. Other Discussions
  3. The Weird and The Wonderful
  4. Does NULL <> 'string'?

Does NULL <> 'string'?

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasehelpquestion
55 Posts 20 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.
  • C Chris Maunder

    You'd think so. Except in SQL We had a query:

    Select count(*)
    From TableOne

    which returned, say, 500,000 records. Next we added

    Select count(*)
    From TableOne
    Left Join TableTwo On TableTwo.TableTwoID = TableOne.TableTwoID
    Where TableTwo.TableTwoID is null or TableTwo.StringColumn <> 'value'

    We're trying to find the number of records in TableOne which, when joined with TableTwo, either have no corresponding TableTwo row or the corresponding TableTwo row is not 'value'. TableTwo.StringColumn is nullable. The result? Adding the join resulted in 25K records. It should have been over 490K records. The issue?

    TableTwo.StringColumn <> 'value'

    This comparison returns false if TableTwo.StringColumn is null. So one needs to use

    IsNull(TableTwo.StringColumn, '') <> 'value'

    to get the correct result.

    cheers Chris Maunder

    O Offline
    O Offline
    obermd
    wrote on last edited by
    #30

    The SQL standards don't define how NULL should behave. What you found is accurate for one SQL dialect but may not be accurate for another. This is actually one of the biggest challenges when changing RDBMS vendors.

    Richard DeemingR 1 Reply Last reply
    0
    • C Chris Maunder

      You'd think so. Except in SQL We had a query:

      Select count(*)
      From TableOne

      which returned, say, 500,000 records. Next we added

      Select count(*)
      From TableOne
      Left Join TableTwo On TableTwo.TableTwoID = TableOne.TableTwoID
      Where TableTwo.TableTwoID is null or TableTwo.StringColumn <> 'value'

      We're trying to find the number of records in TableOne which, when joined with TableTwo, either have no corresponding TableTwo row or the corresponding TableTwo row is not 'value'. TableTwo.StringColumn is nullable. The result? Adding the join resulted in 25K records. It should have been over 490K records. The issue?

      TableTwo.StringColumn <> 'value'

      This comparison returns false if TableTwo.StringColumn is null. So one needs to use

      IsNull(TableTwo.StringColumn, '') <> 'value'

      to get the correct result.

      cheers Chris Maunder

      A Offline
      A Offline
      agolddog
      wrote on last edited by
      #31

      Total guess here, but I wonder if the SQL engine errors on the side of caution. Something like: "I don't have a string to compare to. Thus, I can't say with certainty it is or is not 'value', so I'll return false." But yeah, I think that has gotten us all at one time or another when doing inequality in a query. I also wonder if it varies with different implementations of SQL. But not enough to, you know, test it or anything.

      1 Reply Last reply
      0
      • L Lost User

        Yes, and it is pretty correct. NULL is not defined and can therefore not be compared to a value. Also not for nuallable type in c#.

        M Offline
        M Offline
        Member_5893260
        wrote on last edited by
        #32

        ...except there's an "or" in there: logically, it shouldn't matter: if it's null, at least one of the conditions is true. I have a feeling that "null=string" evaluates to null, and then "field is null" evaluates to Boolean, then "boolean or null" evaluates to null, which is why it doesn't work. But that's somewhat abstruse. Which server is this: is it SQL Server or MySQL... or perhaps something else?

        L Richard DeemingR 2 Replies Last reply
        0
        • M Member_5893260

          ...except there's an "or" in there: logically, it shouldn't matter: if it's null, at least one of the conditions is true. I have a feeling that "null=string" evaluates to null, and then "field is null" evaluates to Boolean, then "boolean or null" evaluates to null, which is why it doesn't work. But that's somewhat abstruse. Which server is this: is it SQL Server or MySQL... or perhaps something else?

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

          But the IS NULL check is made on TableTwo.TableTwoID While string compare is made with TableTwo.StringColumn So I see no incorrect handling, but of course it is not very intuitiv.

          M 1 Reply Last reply
          0
          • C Chris Maunder

            You'd think so. Except in SQL We had a query:

            Select count(*)
            From TableOne

            which returned, say, 500,000 records. Next we added

            Select count(*)
            From TableOne
            Left Join TableTwo On TableTwo.TableTwoID = TableOne.TableTwoID
            Where TableTwo.TableTwoID is null or TableTwo.StringColumn <> 'value'

            We're trying to find the number of records in TableOne which, when joined with TableTwo, either have no corresponding TableTwo row or the corresponding TableTwo row is not 'value'. TableTwo.StringColumn is nullable. The result? Adding the join resulted in 25K records. It should have been over 490K records. The issue?

            TableTwo.StringColumn <> 'value'

            This comparison returns false if TableTwo.StringColumn is null. So one needs to use

            IsNull(TableTwo.StringColumn, '') <> 'value'

            to get the correct result.

            cheers Chris Maunder

            K Offline
            K Offline
            Kirk 10389821
            wrote on last edited by
            #34

            You stumbled on the NULL value special case. NOTHING can be compared to NULL. In fact NULL <> NULL... ISNULL() is the correct way, or (X IS NULL) It makes for difficult code to write with dynamic where clauses on nullable fields. I have seen SOME DBs that they treat '' as NULL and it is worse with a varchar field that trims trailing spaces, and someone stores a space. This is BY DESIGN as explained, and part of the "CALCULUS" of the system. ALSO (1 = NULL) is NOT false... It is NULL That prevents inverting the logic to "NOT (1 = NULL)" Learn that ONCE, learn it well, and life becomes easier.

            C 1 Reply Last reply
            0
            • K Kirk 10389821

              You stumbled on the NULL value special case. NOTHING can be compared to NULL. In fact NULL <> NULL... ISNULL() is the correct way, or (X IS NULL) It makes for difficult code to write with dynamic where clauses on nullable fields. I have seen SOME DBs that they treat '' as NULL and it is worse with a varchar field that trims trailing spaces, and someone stores a space. This is BY DESIGN as explained, and part of the "CALCULUS" of the system. ALSO (1 = NULL) is NOT false... It is NULL That prevents inverting the logic to "NOT (1 = NULL)" Learn that ONCE, learn it well, and life becomes easier.

              C Offline
              C Offline
              Chris Maunder
              wrote on last edited by
              #35

              I've learned it many time. My brain, however, refuses to store it in long term memory.

              cheers Chris Maunder

              1 Reply Last reply
              0
              • N Nelek

                OriginalGriff wrote:

                Dali-esque

                Surrealistic? or womenizer? :laugh: :laugh: :laugh: :laugh:

                M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #36

                Nelek wrote:

                womenizer?

                My experience is that SQL lacks, er, discrimination in who it molests.

                Software Zen: delete this;

                1 Reply Last reply
                0
                • M Member_5893260

                  ...except there's an "or" in there: logically, it shouldn't matter: if it's null, at least one of the conditions is true. I have a feeling that "null=string" evaluates to null, and then "field is null" evaluates to Boolean, then "boolean or null" evaluates to null, which is why it doesn't work. But that's somewhat abstruse. Which server is this: is it SQL Server or MySQL... or perhaps something else?

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #37

                  Dan Sutton wrote:

                  I have a feeling that "null=string" evaluates to null, and then "field is null" evaluates to Boolean, then "boolean or null" evaluates to null, which is why it doesn't work.

                  Pretty much, but it depends on the operator and the boolean value. If it's possible to short-circuit the operator, it doesn't matter if one condition is Null.

                  AND | True | False | Null

                  True | True | False | Null
                  False | False | False | False
                  Null | Null | False | Null

                  OR | True | False | Null

                  True | True | True | True
                  False | True | False | Null
                  Null | True | Null | Null

                  It's sometimes easier to think of Null as "unknown". :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    But the IS NULL check is made on TableTwo.TableTwoID While string compare is made with TableTwo.StringColumn So I see no incorrect handling, but of course it is not very intuitiv.

                    M Offline
                    M Offline
                    Member_5893260
                    wrote on last edited by
                    #38

                    Yeah - you're right. Hadn't had coffee yet. LOL!

                    L 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Dan Sutton wrote:

                      I have a feeling that "null=string" evaluates to null, and then "field is null" evaluates to Boolean, then "boolean or null" evaluates to null, which is why it doesn't work.

                      Pretty much, but it depends on the operator and the boolean value. If it's possible to short-circuit the operator, it doesn't matter if one condition is Null.

                      AND | True | False | Null

                      True | True | False | Null
                      False | False | False | False
                      Null | Null | False | Null

                      OR | True | False | Null

                      True | True | True | True
                      False | True | False | Null
                      Null | True | Null | Null

                      It's sometimes easier to think of Null as "unknown". :)


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      M Offline
                      M Offline
                      Member_5893260
                      wrote on last edited by
                      #39

                      I know: I failed to see first time around that the comparisons were made on separate columns. Coffee. It's a mandatory component first thing in the morning. Of course, not having had any, I failed to realise that, too!

                      1 Reply Last reply
                      0
                      • O obermd

                        The SQL standards don't define how NULL should behave. What you found is accurate for one SQL dialect but may not be accurate for another. This is actually one of the biggest challenges when changing RDBMS vendors.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #40

                        obermd wrote:

                        The SQL standards don't define how NULL should behave.

                        Yes they do. SQL-92 specifically states:

                        SQL-92, section 8.2[^]:

                        If XV or YV is the null value, then "X <comp op> Y" is un-known.

                        That's the standard behaviour with SET ANSI_NULLS ON (the default).


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        1 Reply Last reply
                        0
                        • M Member_5893260

                          Yeah - you're right. Hadn't had coffee yet. LOL!

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

                          :laugh:

                          1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            It's simple three-valued Boolean logic - what's the problem?! :laugh:

                            Chris Maunder wrote:

                            IsNull(TableTwo.StringColumn, '') <> 'value'

                            Except that's not SARGabale.

                            WHERE
                            TableTwo.StringColumn Is Null
                            Or
                            TableTwo.StringColumn != 'value'

                            (You can remove TableTwo.TableTwoID Is Null, because if that's true, TableTwo.StringColumn will also be Null.)


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                            And avoid "!=" in SQL :laugh:

                            Richard DeemingR 1 Reply Last reply
                            0
                            • L Lost User

                              And avoid "!=" in SQL :laugh:

                              Richard DeemingR Offline
                              Richard DeemingR Offline
                              Richard Deeming
                              wrote on last edited by
                              #43

                              Only if you're anal about standards-compliance. ;P

                              sql - Should I use != or <> for not equal in TSQL? - Stack Overflow[^]:

                              Databases that support both != and <>:

                              • MySQL 5.1
                              • PostgreSQL 8.3
                              • SQLite
                              • Oracle 10g
                              • Microsoft SQL Server 2000/2005/2008/2012/2016
                              • IBM Informix Dynamic Server 10
                              • InterBase/Firebird
                              • Apache Derby 10.6
                              • Sybase Adaptive Server Enterprise 11.0

                              Databases that support the ANSI standard operator, exclusively:

                              • IBM DB2 UDB 9.5
                              • Microsoft Access 2010

                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                              L 1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                Only if you're anal about standards-compliance. ;P

                                sql - Should I use != or <> for not equal in TSQL? - Stack Overflow[^]:

                                Databases that support both != and <>:

                                • MySQL 5.1
                                • PostgreSQL 8.3
                                • SQLite
                                • Oracle 10g
                                • Microsoft SQL Server 2000/2005/2008/2012/2016
                                • IBM Informix Dynamic Server 10
                                • InterBase/Firebird
                                • Apache Derby 10.6
                                • Sybase Adaptive Server Enterprise 11.0

                                Databases that support the ANSI standard operator, exclusively:

                                • IBM DB2 UDB 9.5
                                • Microsoft Access 2010

                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                                What the hell... Thank you for the info. Unfortunatelly I have to work mostly with interbase (we are on the way to replace it by MSSQL)and with Interbase until now I thought "!=" is not possible. Made just a short test, and yes also IB accpets "!=" :-O :laugh: [Edit] BTW: Have some Problems to seriously translate "if you're anal compliance" :confused: :laugh:

                                Richard DeemingR 1 Reply Last reply
                                0
                                • L Lost User

                                  What the hell... Thank you for the info. Unfortunatelly I have to work mostly with interbase (we are on the way to replace it by MSSQL)and with Interbase until now I thought "!=" is not possible. Made just a short test, and yes also IB accpets "!=" :-O :laugh: [Edit] BTW: Have some Problems to seriously translate "if you're anal compliance" :confused: :laugh:

                                  Richard DeemingR Offline
                                  Richard DeemingR Offline
                                  Richard Deeming
                                  wrote on last edited by
                                  #45

                                  0x01AA wrote:

                                  BTW: Have some Problems to seriously translate "if you're anal about compliance"

                                  anal - Wiktionary[^] - definition #3: of a person, compulsive and stubborn, obsessed with neatness and accuracy Synonyms: fussy, pernickety, picky


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                  L 1 Reply Last reply
                                  0
                                  • Richard DeemingR Richard Deeming

                                    0x01AA wrote:

                                    BTW: Have some Problems to seriously translate "if you're anal about compliance"

                                    anal - Wiktionary[^] - definition #3: of a person, compulsive and stubborn, obsessed with neatness and accuracy Synonyms: fussy, pernickety, picky


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                                    Thank you for help me learning English. What such a insignifican word like "about" can change everything :laugh: Thank you Bruno

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      You'd think so. Except in SQL We had a query:

                                      Select count(*)
                                      From TableOne

                                      which returned, say, 500,000 records. Next we added

                                      Select count(*)
                                      From TableOne
                                      Left Join TableTwo On TableTwo.TableTwoID = TableOne.TableTwoID
                                      Where TableTwo.TableTwoID is null or TableTwo.StringColumn <> 'value'

                                      We're trying to find the number of records in TableOne which, when joined with TableTwo, either have no corresponding TableTwo row or the corresponding TableTwo row is not 'value'. TableTwo.StringColumn is nullable. The result? Adding the join resulted in 25K records. It should have been over 490K records. The issue?

                                      TableTwo.StringColumn <> 'value'

                                      This comparison returns false if TableTwo.StringColumn is null. So one needs to use

                                      IsNull(TableTwo.StringColumn, '') <> 'value'

                                      to get the correct result.

                                      cheers Chris Maunder

                                      H Offline
                                      H Offline
                                      hpcoder2
                                      wrote on last edited by
                                      #47

                                      Actually, the same sort of thing happens in floating point arithmetic, in pretty much any language under the sun. The values NaN (yes, there's more than one type of NaN) always return false when compared with any other number. So both NaN==x and NaN != x are false. etc., etc. This has bitten me in the arse a number of times, and I work in C++.

                                      1 Reply Last reply
                                      0
                                      • C Chris Maunder

                                        You'd think so. Except in SQL We had a query:

                                        Select count(*)
                                        From TableOne

                                        which returned, say, 500,000 records. Next we added

                                        Select count(*)
                                        From TableOne
                                        Left Join TableTwo On TableTwo.TableTwoID = TableOne.TableTwoID
                                        Where TableTwo.TableTwoID is null or TableTwo.StringColumn <> 'value'

                                        We're trying to find the number of records in TableOne which, when joined with TableTwo, either have no corresponding TableTwo row or the corresponding TableTwo row is not 'value'. TableTwo.StringColumn is nullable. The result? Adding the join resulted in 25K records. It should have been over 490K records. The issue?

                                        TableTwo.StringColumn <> 'value'

                                        This comparison returns false if TableTwo.StringColumn is null. So one needs to use

                                        IsNull(TableTwo.StringColumn, '') <> 'value'

                                        to get the correct result.

                                        cheers Chris Maunder

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

                                        Chris Maunder wrote:

                                        to get the correct result.

                                        The other result was also correct, just not the one you wanted. The behaviour is logical. Your last example would be very VB-ish, where a "NULL" value is treated like an empty string. In the database-world, an empty value does not mean an empty string. ..and it is not something recent, is it? :p

                                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                                        C 1 Reply Last reply
                                        0
                                        • L Lost User

                                          Chris Maunder wrote:

                                          to get the correct result.

                                          The other result was also correct, just not the one you wanted. The behaviour is logical. Your last example would be very VB-ish, where a "NULL" value is treated like an empty string. In the database-world, an empty value does not mean an empty string. ..and it is not something recent, is it? :p

                                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                                          C Offline
                                          C Offline
                                          Chris Maunder
                                          wrote on last edited by
                                          #49

                                          Eddy Vluggen wrote:

                                          The other result was also correct, just not the one you wanted

                                          This is exactly why I love programming.

                                          cheers Chris Maunder

                                          T 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