{"id":8623,"date":"2020-05-18T08:53:07","date_gmt":"2020-05-17T22:53:07","guid":{"rendered":"https:\/\/database.guide\/?p=8623"},"modified":"2020-05-28T13:44:03","modified_gmt":"2020-05-28T03:44:03","slug":"how-sqlite-ifnull-works","status":"publish","type":"post","link":"https:\/\/database.guide\/how-sqlite-ifnull-works\/","title":{"rendered":"How SQLite Ifnull() Works"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/database.guide\/what-is-sqlite\/\">SQLite<\/a> <code>ifnull()<\/code> function allows you to replace NULL values with another value.<\/p>\n\n\n\n<p>It takes two arguments, and it returns a copy of its first non-NULL argument, or NULL if both arguments are NULL. <\/p>\n\n\n\n<p>The <code>ifnull()<\/code> function is equivalent to <code><a href=\"https:\/\/database.guide\/how-coalesce-works-in-sqlite\/\" class=\"aioseop-link\">coalesce()<\/a><\/code> with two arguments.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Simple Example<\/h2>\n\n\n\n<p>Here&#8217;s a simple example to demonstrate the concept.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n  ifnull(123, 0),\n  ifnull(NULL, 0);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ifnull(123, 0)&nbsp; ifnull(NULL, 0)\n--------------&nbsp; ---------------\n123 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<p>The second column was NULL and so <strong>0<\/strong> was returned instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Database Example<\/h2>\n\n\n\n<p>This example uses <code>ifnull()<\/code> in a database query. This demonstrates how <code>ifnull()<\/code> can be useful when querying data that may contain NULL values.<\/p>\n\n\n\n<p>Take the following table as an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ProductId &nbsp; ProductName&nbsp; &nbsp; Price&nbsp; &nbsp; &nbsp;\n----------&nbsp; -------------&nbsp; ----------\n1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Widget Holder&nbsp; 139.5&nbsp; &nbsp; &nbsp;\n2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Widget Stick &nbsp; 89.75&nbsp; &nbsp; &nbsp;\n3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Foo Cap&nbsp; &nbsp; &nbsp; &nbsp; 11.99&nbsp; &nbsp; &nbsp;\n4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Free Widget&nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp;\n5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Free Foobar&nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp;\n6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Free Beer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<p>Most products have been populated with prices, but the price of <strong>Free Beer<\/strong> is NULL.<\/p>\n\n\n\n<p>We can change this to a value of our choosing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n  ProductName,\n  ifnull(Price, 0.0)\nFROM Products;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ProductName&nbsp; &nbsp; ifnull(Price, 0.0)\n-------------&nbsp; ------------------\nWidget Holder&nbsp; 139.5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nWidget Stick &nbsp; 89.75&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFoo Cap&nbsp; &nbsp; &nbsp; &nbsp; 11.99&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFree Widget&nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFree Foobar&nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFree Beer&nbsp; &nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<p>Now it has the same price as the other free products.<\/p>\n\n\n\n<p>The replacement value doesn&#8217;t necessarily need to be a number. You can also replace it with a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n  ProductName,\n  ifnull(Price, 'FREE!')\nFROM Products;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ProductName&nbsp; &nbsp; ifnull(Price, 'FREE!')\n-------------&nbsp; ----------------------\nWidget Holder&nbsp; 139.5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nWidget Stick &nbsp; 89.75&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFoo Cap&nbsp; &nbsp; &nbsp; &nbsp; 11.99&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFree Widget&nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFree Foobar&nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\nFree Beer&nbsp; &nbsp; &nbsp; FREE!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When Both Arguments are NULL<\/h2>\n\n\n\n<p>If both arguments are NULL, then NULL is returned.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT ifnull(NULL, NULL);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ifnull(NULL, NULL)\n------------------\n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The SQLite ifnull() function allows you to replace NULL values with another value. It takes two arguments, and it returns a copy of its first non-NULL argument, or NULL if both arguments are NULL. The ifnull() function is equivalent to coalesce() with two arguments.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[83,93],"class_list":["post-8623","post","type-post","status-publish","format-standard","hentry","category-sqlite","tag-comparison-functions","tag-functions"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=8623"}],"version-history":[{"count":6,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8623\/revisions"}],"predecessor-version":[{"id":8826,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8623\/revisions\/8826"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=8623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=8623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=8623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}