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

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

    Chris Maunder wrote:

    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'.

    Either I've misunderstood your requirement, or that query isn't technically correct. The query will find the records in TableOne which don't have any corresponding rows in TableTwo, and the records in TableOne which have at least one corresponding record in TableTwo where StringColumn is Null or not equal to 'value'. If there are multiple matching rows in TableTwo, then the rows from TableOne will be repeated. Based on the description, I'd have thought something like this would be a better fit:

    SELECT Count(1)
    FROM TableOne
    WHERE Not Exists
    (
    SELECT 1
    FROM TableTwo
    WHERE TableTwo.TableTwoID = TableOne.TableTwoID
    And TableTwo.StringColumn = 'value'
    )

    That finds all the rows in TableOne which don't have a corresponding row in TableTwo with StringColumn set to 'value'.


    "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

    J 1 Reply Last reply
    0
    • C Chris Maunder

      Richard Deeming wrote:

      It's simple three-valued Boolean logic

      True, False, and Surprise.

      cheers Chris Maunder

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

      True, False, and FileNotFound. :-D What Is Truth? - The Daily WTF[^]


      "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
      • 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

        J Offline
        J Offline
        Jeremy Falcon
        wrote on last edited by
        #8

        I feel ya man. NULL has always been a special case though. It's the bastard stepchild of values. Isn't technology fun?

        Jeremy Falcon

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Chris Maunder wrote:

          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'.

          Either I've misunderstood your requirement, or that query isn't technically correct. The query will find the records in TableOne which don't have any corresponding rows in TableTwo, and the records in TableOne which have at least one corresponding record in TableTwo where StringColumn is Null or not equal to 'value'. If there are multiple matching rows in TableTwo, then the rows from TableOne will be repeated. Based on the description, I'd have thought something like this would be a better fit:

          SELECT Count(1)
          FROM TableOne
          WHERE Not Exists
          (
          SELECT 1
          FROM TableTwo
          WHERE TableTwo.TableTwoID = TableOne.TableTwoID
          And TableTwo.StringColumn = 'value'
          )

          That finds all the rows in TableOne which don't have a corresponding row in TableTwo with StringColumn set to 'value'.


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

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #9

          I think this version reads cleaner (and without the quirks), but I'd be willing to bet the non-sub select version would run quicker.

          Jeremy Falcon

          T J 2 Replies Last reply
          0
          • J Jeremy Falcon

            I think this version reads cleaner (and without the quirks), but I'd be willing to bet the non-sub select version would run quicker.

            Jeremy Falcon

            T Offline
            T Offline
            TheGreatAndPowerfulOz
            wrote on last edited by
            #10

            Jeremy Falcon wrote:

            would run quicker

            Indeed.

            #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

            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

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #11

              This proves SQL is not a real programming language.

              #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

              C 1 Reply Last reply
              0
              • T TheGreatAndPowerfulOz

                This proves SQL is not a real programming language.

                #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

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

                Au contraire. It's the perfect language. Job security forever.

                cheers Chris Maunder

                T E 2 Replies Last reply
                0
                • C Chris Maunder

                  Au contraire. It's the perfect language. Job security forever.

                  cheers Chris Maunder

                  T Offline
                  T Offline
                  TheGreatAndPowerfulOz
                  wrote on last edited by
                  #13

                  LOL. Especially when you have your own company! ;P

                  #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    Au contraire. It's the perfect language. Job security forever.

                    cheers Chris Maunder

                    E Offline
                    E Offline
                    Erick Mattew
                    wrote on last edited by
                    #14

                    Quote:

                    Job security forever

                    Indeed. Writing script, backup,... It's boring but secure.

                    J 1 Reply Last reply
                    0
                    • E Erick Mattew

                      Quote:

                      Job security forever

                      Indeed. Writing script, backup,... It's boring but secure.

                      J Offline
                      J Offline
                      Jeremy Falcon
                      wrote on last edited by
                      #15

                      So is prostitution... but ya know... :~

                      Jeremy Falcon

                      J J 2 Replies Last reply
                      0
                      • J Jeremy Falcon

                        I think this version reads cleaner (and without the quirks), but I'd be willing to bet the non-sub select version would run quicker.

                        Jeremy Falcon

                        J Offline
                        J Offline
                        Jorgen Andersson
                        wrote on last edited by
                        #16

                        Assuming SQL Server, I'll take that bet. NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: SQL Server at EXPLAIN EXTENDED[^] Left outer join vs NOT EXISTS | SQL in the Wild[^] There are always exceptions, but not exists is generally better optimized.

                        Wrong is evil and must be defeated. - Jeff Ello

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          Null most certainly does not equal a valid string value, right? I never realised SQL could be so Javaesque ;)

                          cheers Chris Maunder

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #17

                          It gets Dali-esque as well from time to time...

                          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          N 1 Reply Last reply
                          0
                          • J Jeremy Falcon

                            So is prostitution... but ya know... :~

                            Jeremy Falcon

                            J Offline
                            J Offline
                            Jorgen Andersson
                            wrote on last edited by
                            #18

                            Boring?

                            Wrong is evil and must be defeated. - Jeff Ello

                            J T 2 Replies Last reply
                            0
                            • J Jorgen Andersson

                              Boring?

                              Wrong is evil and must be defeated. - Jeff Ello

                              J Offline
                              J Offline
                              Jeremy Falcon
                              wrote on last edited by
                              #19

                              Some are... so I'm told. :rolleyes:

                              Jeremy Falcon

                              1 Reply Last reply
                              0
                              • J Jorgen Andersson

                                Boring?

                                Wrong is evil and must be defeated. - Jeff Ello

                                T Offline
                                T Offline
                                TheGreatAndPowerfulOz
                                wrote on last edited by
                                #20

                                They don't all "enjoy" it. It's a job. Indeed, prostitutes go same-sex for their personal one-on-ones because opposite-sex has lost all appeal due to the "job".

                                #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                                1 Reply Last reply
                                0
                                • J Jeremy Falcon

                                  So is prostitution... but ya know... :~

                                  Jeremy Falcon

                                  J Offline
                                  J Offline
                                  Jagger B
                                  wrote on last edited by
                                  #21

                                  Sorry, You failed the interview. He'll call you next time. :|

                                  J 1 Reply Last reply
                                  0
                                  • J Jagger B

                                    Sorry, You failed the interview. He'll call you next time. :|

                                    J Offline
                                    J Offline
                                    Jeremy Falcon
                                    wrote on last edited by
                                    #22

                                    So wait, you're saying I can't be a prostitute? Why? Because I'm a guy? That's discrimination.

                                    Jeremy Falcon

                                    J 1 Reply Last reply
                                    0
                                    • J Jeremy Falcon

                                      So wait, you're saying I can't be a prostitute? Why? Because I'm a guy? That's discrimination.

                                      Jeremy Falcon

                                      J Offline
                                      J Offline
                                      Jagger B
                                      wrote on last edited by
                                      #23

                                      Are you saying, Sql Dev's = prostitute?

                                      J 1 Reply Last reply
                                      0
                                      • J Jagger B

                                        Are you saying, Sql Dev's = prostitute?

                                        J Offline
                                        J Offline
                                        Jeremy Falcon
                                        wrote on last edited by
                                        #24

                                        Yes. Everyone knows you have go through prostitute training to get certified. You just finding this out? You may wanna talk to your MS rep.

                                        Jeremy Falcon

                                        N 1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          It gets Dali-esque as well from time to time...

                                          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                          N Offline
                                          N Offline
                                          Nelek
                                          wrote on last edited by
                                          #25

                                          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 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