Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

SQL Making me scratch my noggin, someone explain please!

  • 15-02-2006 01:50PM
    #1
    Registered Users, Registered Users 2 Posts: 2,758 ✭✭✭


    I have a table (TBL_SomeTable) with 4 columns (A , B , C , D). Column D is a bit column that allows nulls.
    Lets say there are 10 rows in TBL_Something: 5 rows that value in D is 1, and 5 rows the value in D is NULL

    IF i do a:
    'select * FROM TBL_Something WHERE D <> 1'
    
    i get no rows returned. Surely the rows where the value of D is null should be returned, as NULL is not equal to 1!

    Now what i did to fix my conundrum was to set a default value of 0 on colunm D so no NULLS appear in the table.

    Can someone please explain whee my logic fell down.


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    To the best of my knowledge, the standard for all SQL queries is not to return any rows where the row on which you are querying returns a null value. That is, NULL is not a comparable value in a row, so SQL doesn't compare NULL and 1, it just ignores it. Thus, it only returns rows which return true for D <> 1, and since rows with a null value in D aren't tested and so do not return a TRUE value, they're not included.

    In order to include the null values, just alter your query to
    'select * FROM TBL_Something WHERE D <> 1 OR D IS NULL'


  • Closed Accounts Posts: 24 Phileas Fogg


    Don't accept NULL values in bit fields and always default them to 0, that should avoid this problem. TBH I don't know why Sql Server allows you to accept NULLs - probably for a reason I haven't run into yet.


  • Registered Users, Registered Users 2 Posts: 2,758 ✭✭✭Peace


    seamus wrote:
    To the best of my knowledge, the standard for all SQL queries is not to return any rows where the row on which you are querying returns a null value. That is, NULL is not a comparable value in a row, so SQL doesn't compare NULL and 1, it just ignores it. Thus, it only returns rows which return true for D <> 1, and since rows with a null value in D aren't tested and so do not return a TRUE value, they're not included.

    In order to include the null values, just alter your query to

    Yup that explanation makes sense seamus, cheers.

    Phileas Fogg i take you point, i was being a bit lazy with my dbase design ;)


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    I was doing this
    'select * FROM TBL_Something WHERE D <> 1 OR D IS NULL'
    
    I would do
    'select * FROM TBL_Something WHERE ISNULL(D,0) <> 1 '
    

    much nicer and removes the nasty or clause mind you <> isn't great either
    and as a side note I much prefer the sql != instead of <>


Advertisement