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. Clever Code
  4. T-SQL: null != value evaluates false [modified]

T-SQL: null != value evaluates false [modified]

Scheduled Pinned Locked Moved Clever Code
databasephpcomtoolsquestion
10 Posts 7 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.
  • S Offline
    S Offline
    Syed Mehroz Alam
    wrote on last edited by
    #1

    We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false. Run the following T-SQL statements and you will get false for both the cases.

    print (Case When null ='Value' Then 'true' else 'false' end)
    print (Case When null**!=**'Value' Then 'true' else 'false' end)

    The query was something like:

    Select ...
    Case When Column1='SomeValue' .. Then ..
    Case When Column1!='SomeValue' .. Then ..
    ..

    I was wondering why none of case statements were evaluating true. Finally, I came to know Column1 resulted in a null due to a left join and then null!='SomeValue' returned false. Syed Mehroz Alam

    My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

    modified on Friday, November 14, 2008 7:12 AM

    J P S 3 Replies Last reply
    0
    • S Syed Mehroz Alam

      We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false. Run the following T-SQL statements and you will get false for both the cases.

      print (Case When null ='Value' Then 'true' else 'false' end)
      print (Case When null**!=**'Value' Then 'true' else 'false' end)

      The query was something like:

      Select ...
      Case When Column1='SomeValue' .. Then ..
      Case When Column1!='SomeValue' .. Then ..
      ..

      I was wondering why none of case statements were evaluating true. Finally, I came to know Column1 resulted in a null due to a left join and then null!='SomeValue' returned false. Syed Mehroz Alam

      My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

      modified on Friday, November 14, 2008 7:12 AM

      J Offline
      J Offline
      Jason Lepack LeppyR64
      wrote on last edited by
      #2

      Syed Mehroz Alam wrote:

      We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false.

      != is still a comparison operator. Example: I have two things on my desk. One is a baseball. Is the other thing a baseball? Is the other thing not a baseball? You don't know, right? Now, I think it would have been more useful to have comparisons containing nulls return null, instead of false. But that is beside the point.

      D 1 Reply Last reply
      0
      • S Syed Mehroz Alam

        We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false. Run the following T-SQL statements and you will get false for both the cases.

        print (Case When null ='Value' Then 'true' else 'false' end)
        print (Case When null**!=**'Value' Then 'true' else 'false' end)

        The query was something like:

        Select ...
        Case When Column1='SomeValue' .. Then ..
        Case When Column1!='SomeValue' .. Then ..
        ..

        I was wondering why none of case statements were evaluating true. Finally, I came to know Column1 resulted in a null due to a left join and then null!='SomeValue' returned false. Syed Mehroz Alam

        My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

        modified on Friday, November 14, 2008 7:12 AM

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Syed Mehroz Alam wrote:

        I was amazed

        I wasn't.

        1 Reply Last reply
        0
        • S Syed Mehroz Alam

          We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false. Run the following T-SQL statements and you will get false for both the cases.

          print (Case When null ='Value' Then 'true' else 'false' end)
          print (Case When null**!=**'Value' Then 'true' else 'false' end)

          The query was something like:

          Select ...
          Case When Column1='SomeValue' .. Then ..
          Case When Column1!='SomeValue' .. Then ..
          ..

          I was wondering why none of case statements were evaluating true. Finally, I came to know Column1 resulted in a null due to a left join and then null!='SomeValue' returned false. Syed Mehroz Alam

          My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

          modified on Friday, November 14, 2008 7:12 AM

          S Offline
          S Offline
          Scott Barbour
          wrote on last edited by
          #4

          Which is why you should use the isnull() function when you want to compare a nullable field to a another field or literal, and field is null (or field is not null) when you want to check for null, though I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.

          I don't claim to be a know it all, for I know that I am not...

          I usually have an answer though.

          S D 2 Replies Last reply
          0
          • S Scott Barbour

            Which is why you should use the isnull() function when you want to compare a nullable field to a another field or literal, and field is null (or field is not null) when you want to check for null, though I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.

            I don't claim to be a know it all, for I know that I am not...

            I usually have an answer though.

            S Offline
            S Offline
            Syed Mehroz Alam
            wrote on last edited by
            #5

            You are right, I also tackled it using IsNull but the original query wasn't written by me and it was hard to find the cause of error since the query was a complex one.

            My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

            A 1 Reply Last reply
            0
            • S Syed Mehroz Alam

              You are right, I also tackled it using IsNull but the original query wasn't written by me and it was hard to find the cause of error since the query was a complex one.

              My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

              A Offline
              A Offline
              alejandrofuchs
              wrote on last edited by
              #6

              Null = Value does not evaluate to false, nor true. It's Unknown. So there are not only two values: True and False, but also Unknown From MSDN: A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.

              1 Reply Last reply
              0
              • J Jason Lepack LeppyR64

                Syed Mehroz Alam wrote:

                We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false.

                != is still a comparison operator. Example: I have two things on my desk. One is a baseball. Is the other thing a baseball? Is the other thing not a baseball? You don't know, right? Now, I think it would have been more useful to have comparisons containing nulls return null, instead of false. But that is beside the point.

                D Offline
                D Offline
                Dag Oystein Johansen
                wrote on last edited by
                #7

                You're certainly right about how it actually does behave. But I don't really see why comparions with null ought to behave differently in SQL than in, say, C#. This whole design decision of using the is operator for comparison to null seems illogical and counterintuitive, but I presume there is some reason for it. If you see a good reason, would you care to elaborate?

                D 1 Reply Last reply
                0
                • S Scott Barbour

                  Which is why you should use the isnull() function when you want to compare a nullable field to a another field or literal, and field is null (or field is not null) when you want to check for null, though I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.

                  I don't claim to be a know it all, for I know that I am not...

                  I usually have an answer though.

                  D Offline
                  D Offline
                  Dag Oystein Johansen
                  wrote on last edited by
                  #8

                  Scott Barbour wrote:

                  I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.

                  If you do not want to represent two different things by null or empty string, why is the field nullable in the first place? In my view, using invariant representations is a virtue. (Of course, there may be some cases where null and empty string is different for some purposed but not others, and then I guess this technique might be useful.)

                  S 1 Reply Last reply
                  0
                  • D Dag Oystein Johansen

                    Scott Barbour wrote:

                    I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.

                    If you do not want to represent two different things by null or empty string, why is the field nullable in the first place? In my view, using invariant representations is a virtue. (Of course, there may be some cases where null and empty string is different for some purposed but not others, and then I guess this technique might be useful.)

                    S Offline
                    S Offline
                    Scott Barbour
                    wrote on last edited by
                    #9

                    I didn't design the tables, I'm just tasked with working with them. If it has never had a value, it is null. If the value has been cleared, it is an empty string. Of course, using the isnull(field,'') is even more useful when you are looking for specific values when nulls are present. It has been my experience with MS SQL Server 2000 that queries behave oddly with nulls (such as excluding rows from the resultset.) especially when using the LIKE operator. Of course MSSQL 2000 has a number of quirks anyway. I've reworked a number of queries that just wouldn't return the correct data.

                    I don't claim to be a know it all, for I know that I am not...

                    I usually have an answer though.

                    1 Reply Last reply
                    0
                    • D Dag Oystein Johansen

                      You're certainly right about how it actually does behave. But I don't really see why comparions with null ought to behave differently in SQL than in, say, C#. This whole design decision of using the is operator for comparison to null seems illogical and counterintuitive, but I presume there is some reason for it. If you see a good reason, would you care to elaborate?

                      D Offline
                      D Offline
                      dave dolan
                      wrote on last edited by
                      #10

                      something can BE null, but it can not be EQUAL TO null. Just like, for example, something (in real life) can be Big, but it cannot be Equal to Big.

                      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