SQL NULL value logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    SQL NULL value logic

    Hey,

    I was playing around with SQL (SQL Server 2008) toady and noticed something I think is really strange:

    Code:
    select 1/null -- returns null
    Why is this operation allowed? Why is any mathematical operation allowed on NULL?

    NULL as I know it in programming is an undefined value. In math, when dividing by 0 you get an undefined value.
    Thus NULL = x/0 ?

    SQL does throw divide by 0 exceptions, so why are division by NULL or comparison to NULL exceptions not raised?

    PS: Please excuse me if this is a silly question, but I generally don't program anything other than the standard CRUD statements in SQL.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    I think this is because any operation against a NULL is NULL. The reason that it is allowed is because the value is "unknown". Since even the server does not know what's inside NULL, it allow the operation and just return an unknown value (NULL)...

    Just my two cents...

    ~~ CK

    Comment

    • cloud255
      Recognized Expert Contributor
      • Jun 2008
      • 427

      #3
      I understand it returning NULL, but that is the essence of my problem with this scenario. You can never retrieve any meaningful value from an operation using a NULL. So why even allow it?
      Most programming languages throw exceptions for exactly this reason, if feel SQL should too.

      Does that mean that every single variable must be checked each time that it is used (calling the isnull() function) and manually raising an exception if the value is NULL? The whole scenario just feels awkward and poorly implemented IMHO.

      Comment

      Working...