{"id":987,"date":"2015-10-12T12:48:05","date_gmt":"2015-10-12T12:48:05","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=987"},"modified":"2015-10-15T13:20:27","modified_gmt":"2015-10-15T13:20:27","slug":"using-nvl-in-select-query","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/using-nvl-in-select-query\/","title":{"rendered":"Using NVL in Select Query"},"content":{"rendered":"<p>When the Column value is NULL it would be replaced by &#8216;~&#8217; in output<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  SELECT NVL(FAX_NO, '~') FROM DUAL;\r\n  SELECT NVL(NULL, '~') FROM DUAL;\r\n<\/pre>\n<p>The Below query can be used in scenarios where 3 drop downs are used and Location is mandatory and loaded first based on which Area and Pincode should be populated and selection can be made based on that later.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  SELECT Name, Age, PhoneNo\r\n    FROM Person \r\n   WHERE Location =  p_location and \r\n         Area =  NVL(p_area, Area) and \r\n         PinCode =  NVL(p_pin_code, PinCode);\r\n<\/pre>\n<p>Having more than one input in where clause<br \/>\n<em>i.e<\/em><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT * \r\n  FROM Person \r\n WHERE Location IN('North Chennai', 'South Chennai');\r\n<\/pre>\n<p>When the same query is used in search screen it may have Three possible Values<\/p>\n<ul>\n<li>NULL<\/li>\n<li>Single Value<\/li>\n<li>Multiple Value<\/li>\n<\/ul>\n<p>The above query works for Single and Multiple value but does not work for NULL.The Above query works for NULL and Single Value but not for Multiple Value.<\/p>\n<p>The below is a simple query which works when PERSON_ID is NULL, Single Value, Multiple Value(CSV).<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT PERSON_ID\r\n  FROM PERSONS P\r\n WHERE ((CASE\r\n         WHEN 'P101' IS NULL THEN\r\n          NULL\r\n         ELSE\r\n          'P101'\r\n       END) IS NULL OR P.WATERFALL_ID IN ('P101'));\r\n<\/pre>\n<p>For Multiple Value we need to do slight modification.We need to convert the CSV values into table and give it as input.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT DISTINCT PERSON_ID\r\n   FROM tblPerson \r\n  WHERE nvl(PERSON_ID, '~') IN (SELECT column_value\r\n                                  FROM TABLE(PACKAGE_NAME.CSV_TO_TABLE(P_CSV_PERSON_ID))\r\n                                UNION ALL\r\n                                SELECT '~'\r\n                                  FROM dual);\r\n<\/pre>\n<p>The select query in the where clause will take the value of <strong>P_CSV_PERSON_ID<\/strong> (Single or Multiple value) else it will take <strong>~<\/strong> in case it is NULL<\/p>\n<p>The Other workaround to this is as below<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT PERSON_ID\r\n    FROM tblPerson \r\n   WHERE (((CASE\r\n           WHEN P_CSV_PERSON_ID IS NULL THEN\r\n            NULL\r\n           ELSE\r\n            P_CSV_PERSON_ID\r\n         END)) IS NULL\r\n      OR PERSON_ID IN\r\n         (SELECT *\r\n            FROM TABLE(PACKAGE_NAME.CSV_TO_TABLE(P_CSV_PERSON_ID))));\r\n<\/pre>\n<p>where <\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n P_CSV_PERSON_ID= 'P101,P102'\r\n<\/pre>\n<p>For more details on <strong>CSV_TO_TABLE<\/strong> refer <a href=\"http:\/\/codethataint.com\/blog\/pipe-row-oracle-function-example\/\">Link<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When the Column value is NULL it would be replaced by &#8216;~&#8217; in output SELECT NVL(FAX_NO, &#8216;~&#8217;) FROM DUAL; SELECT NVL(NULL, &#8216;~&#8217;) FROM DUAL; The Below query can be used in scenarios where 3 drop downs are used and Location is mandatory and loaded first based on which Area and Pincode should be populated and&hellip; <a href=\"https:\/\/codethataint.com\/blog\/using-nvl-in-select-query\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-queries-oracle-database-2"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=987"}],"version-history":[{"count":11,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/987\/revisions\/989"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}