{"id":167,"date":"2007-08-26T19:46:05","date_gmt":"2007-08-26T18:46:05","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=167"},"modified":"2007-08-26T19:46:05","modified_gmt":"2007-08-26T18:46:05","slug":"tri-state-boolean","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=167","title":{"rendered":"Tri-state boolean"},"content":{"rendered":"<p>All data types in the <a title=\"Oracle 11g, Siebel, PeopleSoft | Oracle, The World's Largest Enterprise Software Company\" href=\"http:\/\/www.oracle.com\" target=\"_blank\">Oracle<\/a> database implement the null value. Null is a very special value which means &#8216;Unknown&#8217;. That means that the value is really unknown. Null is <strong>not<\/strong> equal to null, because both are not known. Therefore you cannot compare them to each other.<\/p>\n<p>Even the boolean value has this null value implemented. Where you should expect a boolean value to hold either <em>true<\/em> or <em>false<\/em>, in Oracle a boolean value can also hold null as a value. So a boolean value has three possibilities instead of the two you normally expect.<\/p>\n<p><!--more--><\/p>\n<p>Normally you would have this truth table for comparing boolean values:<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"200\" border=\"0\" unselectable=\"on\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"49\"><strong>left<\/strong><\/td>\n<td valign=\"top\" width=\"51\"><strong>right<\/strong><\/td>\n<td valign=\"top\" width=\"49\"><strong>and<\/strong><\/td>\n<td valign=\"top\" width=\"49\"><strong>or<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"51\">true<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"51\">false<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"51\">true<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"54\">false<\/td>\n<td valign=\"top\" width=\"57\">false<\/td>\n<td valign=\"top\" width=\"64\">false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>According to <a title=\"NULL in Oracle\" href=\"http:\/\/www.adp-gmbh.ch\/ora\/misc\/null.html\" target=\"_blank\">this<\/a> page, you have the following truth table for comparing boolean values:<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"200\" border=\"0\" unselectable=\"on\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"49\"><strong>left<\/strong><\/td>\n<td valign=\"top\" width=\"51\"><strong>right<\/strong><\/td>\n<td valign=\"top\" width=\"49\"><strong>and<\/strong><\/td>\n<td valign=\"top\" width=\"49\"><strong>or<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"51\">true<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"51\">false<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">true<\/td>\n<td valign=\"top\" width=\"51\">null<\/td>\n<td valign=\"top\" width=\"49\">null<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"51\">true<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"51\">false<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"51\">null<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">null<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">null<\/td>\n<td valign=\"top\" width=\"51\">true<\/td>\n<td valign=\"top\" width=\"49\">null<\/td>\n<td valign=\"top\" width=\"49\">true<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">null<\/td>\n<td valign=\"top\" width=\"51\">false<\/td>\n<td valign=\"top\" width=\"49\">false<\/td>\n<td valign=\"top\" width=\"49\">null<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"49\">null<\/td>\n<td valign=\"top\" width=\"54\">null<\/td>\n<td valign=\"top\" width=\"57\">null<\/td>\n<td valign=\"top\" width=\"64\">null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This means you either have to build you software around these possibilities or you can treat the null value as if it were false, i.e. just test for true value. This is mostly how Oracle PL\/SQL treats this value. In an if statement everything that results in null, will mean the <em>else<\/em> branch will be executed.<\/p>\n<p>I have for example an overloaded function in my utilities package implementing an ifelse function.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b5440eb8-f6e2-44a1-8110-df2ef7235464\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">FUNCTION<\/span><span style=\"color: #000000; \"> ifelse(\n    if_in                          <\/span><span style=\"color: #0000FF; \">IN<\/span><span style=\"color: #000000; \">      <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">\n  , then_in                        <\/span><span style=\"color: #0000FF; \">IN<\/span><span style=\"color: #000000; \">      <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">\n  , else_in                        <\/span><span style=\"color: #0000FF; \">IN<\/span><span style=\"color: #000000; \">      <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \"> )\n    <\/span><span style=\"color: #0000FF; \">RETURN<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">;\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>this function is overloaded for different datatypes, but the implementation is roughly the same for all versions:<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:c4772a80-2c82-4343-845a-3b046d01a480\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">FUNCTION<\/span><span style=\"color: #000000; \"> ifelse(\n    if_in                          <\/span><span style=\"color: #0000FF; \">IN<\/span><span style=\"color: #000000; \">      <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">\n  , then_in                        <\/span><span style=\"color: #0000FF; \">IN<\/span><span style=\"color: #000000; \">      <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">\n  , else_in                        <\/span><span style=\"color: #0000FF; \">IN<\/span><span style=\"color: #000000; \">      <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \"> )\n    <\/span><span style=\"color: #0000FF; \">RETURN<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">\n  <\/span><span style=\"color: #0000FF; \">IS<\/span><span style=\"color: #000000; \">\n    lv_returnvalue                           <\/span><span style=\"color: #0000FF; \">BOOLEAN<\/span><span style=\"color: #000000; \">;\n  <\/span><span style=\"color: #0000FF; \">BEGIN<\/span><span style=\"color: #000000; \">\n    <\/span><span style=\"color: #0000FF; \">IF<\/span><span style=\"color: #000000; \"> if_in <\/span><span style=\"color: #0000FF; \">THEN<\/span><span style=\"color: #000000; \">\n      lv_returnvalue   :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> then_in;\n    <\/span><span style=\"color: #0000FF; \">ELSE<\/span><span style=\"color: #000000; \">\n      lv_returnvalue   :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> else_in;\n    <\/span><span style=\"color: #0000FF; \">END<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">IF<\/span><span style=\"color: #000000; \">;\n\n    <\/span><span style=\"color: #0000FF; \">RETURN<\/span><span style=\"color: #000000; \"> lv_returnvalue;\n  <\/span><span style=\"color: #0000FF; \">END<\/span><span style=\"color: #000000; \">;\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>This implementation treats every value other than true as a false value. So, if I use this function if will have a work around for the null value and I can use boolean values as if they were (normal) two-state booleans. I can, for instance, use this function as a wrapper to work around possible null values in my if-statements.<\/p>\n<p>Where I would have currently have this code:<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a1b6e2ce-d084-4f65-a907-f88bdbbea020\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">if<\/span><span style=\"color: #000000; \"> (a <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> b) <\/span><span style=\"color: #0000FF; \">and<\/span><span style=\"color: #000000; \"> (c <\/span><span style=\"color: #000000; \">&lt;&gt;<\/span><span style=\"color: #000000; \"> d)  <\/span><span style=\"color: #0000FF; \">then<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>where the values of the variables could be null independently,&nbsp;I would make this code:<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:121c7b39-d644-42a1-b10d-2d272642433a\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">if<\/span><span style=\"color: #000000; \"> bar$util.ifelse((a <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> b) <\/span><span style=\"color: #0000FF; \">and<\/span><span style=\"color: #000000; \"> (c <\/span><span style=\"color: #000000; \">&lt;&gt;<\/span><span style=\"color: #000000; \"> d),<\/span><span style=\"color: #0000FF; \">true<\/span><span style=\"color: #000000; \">,<\/span><span style=\"color: #0000FF; \">false<\/span><span style=\"color: #000000; \">)  <\/span><span style=\"color: #0000FF; \">then<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>Now I am sure that the null values are treated as if they were false and I don&#8217;t have to worry about what the values really are.<\/p>\n<style type=\"text\/css\">.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }\n<\/style>\n","protected":false},"excerpt":{"rendered":"<p>All data types in the Oracle database implement the null value. Null is a very special value which means &#8216;Unknown&#8217;. That means that the value is really unknown. Null is not equal to null, because both are not known. Therefore you cannot compare them to each other. Even the boolean value has this null value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}