{"id":50,"date":"2008-01-16T21:58:03","date_gmt":"2008-01-16T19:58:03","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/01\/16\/comparisons-with-null\/"},"modified":"2008-01-16T21:58:03","modified_gmt":"2008-01-16T19:58:03","slug":"comparisons-with-null","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/01\/16\/comparisons-with-null\/","title":{"rendered":"Comparisons with NULL"},"content":{"rendered":"<p>Or &#8216;<em>True, False or FileNotFound&#8217; <\/em><sup>(1)<\/sup><\/p>\n<p>Null seems to  confuse a lot of people. The main issue with null is that it is not a value. It&#8217;s the absence of a value, a place holder for unknown data. Hence, when it is used in a comparison, it returns results that look surprising. Take this as a quick example<\/p>\n<p>DECLARE @Var1 INT<br \/>\nSET @Var1 = NULL<\/p>\n<p>SELECT<br \/>\nCASE WHEN @Var1 = NULL THEN &#8216;Var1 = Null&#8217; ELSE &#8216;Var1 &lt;&gt; NULL&#8217; END AS EqualityTest,<br \/>\nCASE WHEN @Var1 &lt;&gt; NULL THEN &#8216;Var1 &lt;&gt; Null&#8217; ELSE &#8216;Var1 = NULL&#8217; END AS InequalityTest<\/p>\n<p>The results look a little strange. They&#8217;re supposed to. (No, I didn&#8217;t mean IS NULL\/IS NOT NULL)<\/p>\n<p><!--more--> SQL uses what&#8217;s know as three-state logic. A boolean expression can be true, false or NULL. If any of the values in the expression are NULL, it will return NULL. This has interesting implications for conditions in a query.<\/p>\n<p>Let&#8217;s take a simple table that has a value column with numbers from 1-10 and null (creation code at the end of the article)<\/p>\n<p>A condition WHERE Value = 5 will, as expected, only return the rows where value = 5<br \/>\nA condition WHERE Value &lt;&gt; 5 will return  the values 1-4 and 6-10, but not NULL.<br \/>\nOnly a condition of WHERE Value IS NULL will return the null row. Anything else eliminates the null row.<\/p>\n<p>Why is this so important? Well, often I see code like the following:<\/p>\n<p>SELECT &#8230;<br \/>\nWHERE ISNULL(SomeStringColumn,&#8221;) &lt;&gt; &#8221; <sup>(2)<\/sup><\/p>\n<p>Looks fairly simple. Returns all rows that don&#8217;t have null or empty string in the string column. Fair enough, it does work. There&#8217;s one big problem with that though. The function on the column prevents index usage (assuming there&#8217;s an index on the column). It&#8217;s also totally unnecessary.<\/p>\n<p>As shown above, any condition on a column (except IS NULL) will eliminate null rows. So that query could be expressed as<\/p>\n<p>SELECT &#8230;<br \/>\nWHERE SomeStringColumn &lt;&gt; &#8221;<\/p>\n<p>and it will return exactly the same values, will allow effective index usage, and won&#8217;t force the CPU to do unnecessary computations.<\/p>\n<p>(1) <a href=\"http:\/\/thedailywtf.com\/Articles\/What_Is_Truth_0x3f_.aspx\">What is Truth?<\/a><br \/>\n(2) Thank to <a href=\"http:\/\/www.sqlservercentral.com\/Forums\/UserInfo85377.aspx\">Jeff Moden<\/a> from <a href=\"http:\/\/www.sqlservercentral.com\/\">SQLServerCentral.com<\/a> who initially brought this query and issue up.<\/p>\n<hr width=\"20%\" \/> <code>Create Table #TestingNulls (<br \/>\nValue INT<br \/>\n)<br \/>\nGO<\/code><code>Insert into #TestingNulls VALUES (1)<br \/>\nInsert into #TestingNulls VALUES (2)<br \/>\nInsert into #TestingNulls VALUES (3)<br \/>\nInsert into #TestingNulls VALUES (4)<br \/>\nInsert into #TestingNulls VALUES (5)<br \/>\nInsert into #TestingNulls VALUES (6)<br \/>\nInsert into #TestingNulls VALUES (7)<br \/>\nInsert into #TestingNulls VALUES (8)<br \/>\nInsert into #TestingNulls VALUES (9)<br \/>\nInsert into #TestingNulls VALUES (10)<br \/>\nInsert into #TestingNulls VALUES (NULL)<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Or &#8216;True, False or FileNotFound&#8217; (1) Null seems to confuse a lot of people. The main issue with null is that it is not a value. It&#8217;s the absence of a value, a place holder for unknown data. Hence, when&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/01\/16\/comparisons-with-null\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,17],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-t-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-O","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}