{"id":4991,"date":"2024-11-14T11:17:32","date_gmt":"2024-11-14T03:17:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/4991\/"},"modified":"2024-11-14T11:17:32","modified_gmt":"2024-11-14T03:17:32","slug":"oracle-nvl2%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/4991\/","title":{"rendered":"oracle nvl2\u51fd\u6570\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  nvl2 \u51fd\u6570\u6839\u636e\u6761\u4ef6\u8fd4\u56de\u4e24\u4e2a\u4e0d\u540c\u503c\uff1acondition \u4e3a true\uff0c\u8fd4\u56de value_if_true\uff1bcondition \u4e3a false\uff0c\u8fd4\u56de value_if_false\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/21\/2024052109002678929.jpg\" class=\"aligncenter\" title=\"oracle nvl2\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"oracle nvl2\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>NVL2 \u51fd\u6570\u8be6\u89e3<\/strong><\/p>\n<p>NVL2 \u51fd\u6570\u662f\u4e00\u4e2a Oracle \u51fd\u6570\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fd4\u56de\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>NVL2(condition, value_if_true, value_if_false)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>condition<\/strong>\uff1a\u8981\u8bc4\u4f30\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f<\/li>\n<li> <strong>value_if_true<\/strong>\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u771f\u65f6\u8981\u8fd4\u56de\u7684\u503c<\/li>\n<li> <strong>value_if_false<\/strong>\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u5047\u65f6\u8981\u8fd4\u56de\u7684\u503c<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>NVL2 \u51fd\u6570\u901a\u8fc7\u8bc4\u4f30\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u786e\u5b9a\u8981\u8fd4\u56de\u7684\u503c\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u8fd4\u56de value_if_true\uff1b\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u8fd4\u56de value_if_false\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 NVL2 \u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>SELECT NVL2(salary &gt; 50000, 'High Salary', 'Low Salary') AS salary_status\nFROM employees;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u4f1a\u8fd4\u56de\u4e00\u4e2a\u540d\u4e3a salary_status \u7684\u65b0\u5217\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u503c\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u5458\u5de5\u7684\u5de5\u8d44\u9ad8\u4e8e 50,000\uff0c\u5219\u4e3a &#8220;High Salary&#8221;<\/li>\n<li>\u5426\u5219\uff0c\u4e3a &#8220;Low Salary&#8221;<\/li>\n<\/ul>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>NVL2 \u51fd\u6570\u901a\u5e38\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u6761\u4ef6\u5c06\u6570\u636e\u5206\u7c7b<\/li>\n<li>\u66ff\u6362 NULL \u503c<\/li>\n<li>\u63d0\u4f9b\u4e0d\u540c\u7684\u6d88\u606f\u6216\u8f93\u51fa\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6761\u4ef6<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>condition \u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684 SQL \u8868\u8fbe\u5f0f\u3002<\/li>\n<li>value_if_true \u548c value_if_false \u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5fc5\u987b\u517c\u5bb9\u3002<\/li>\n<li>\u5982\u679c\u6761\u4ef6\u4e3a NULL\uff0c\u5219 NVL2 \u51fd\u6570\u5c06\u8fd4\u56de NULL\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5176\u4ed6\u4f7f\u7528 NVL2 \u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<ul>\n<li><strong>\u5c06 NULL \u503c\u66ff\u6362\u4e3a\u9ed8\u8ba4\u503c\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT NVL2(name, 'Unknown', name) AS full_name\nFROM customers;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li><strong>\u6839\u636e\u6027\u522b\u8fd4\u56de\u4e0d\u540c\u7684\u6d88\u606f\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT NVL2(gender, 'Male', 'Female') AS gender_description\nFROM employees;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li><strong>\u6839\u636e\u6761\u4ef6\u8ba1\u7b97\u6298\u6263\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT NVL2(quantity &gt;= 10, 0.1, 0.05) AS discount_rate\nFROM sales;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f nvl2\u51fd\u6570\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nvl2 \u51fd\u6570\u6839\u636e\u6761\u4ef6\u8fd4\u56de\u4e24\u4e2a\u4e0d\u540c\u503c\uff1acondition \u4e3a true\uff0c\u8fd4\u56de value_if_true\uff1bcondition \u4e3a false\uff0c\u8fd4\u56de value_if_false\u3002 NVL2 \u51fd\u6570\u8be6\u89e3 NVL2 \u51fd\u6570\u662f\u4e00\u4e2a Oracle \u51fd\u6570\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fd4\u56de\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a NVL2(condition, value_if_true, value_if_false) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a condition\uff1a\u8981\u8bc4\u4f30\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f value_if_true\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u771f\u65f6\u8981\u8fd4\u56de\u7684\u503c value_if_false\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u5047\u65f6\u8981\u8fd4\u56de\u7684\u503c \u7528\u6cd5 NVL2 \u51fd\u6570\u901a\u8fc7\u8bc4\u4f30\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u786e\u5b9a\u8981\u8fd4\u56de\u7684\u503c\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u8fd4\u56de value_if_true\uff1b\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u8fd4\u56de value_if_false\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 NVL2 \u51fd\u6570\u7684\u793a\u4f8b\uff1a SELECT NVL2(salary &gt; 50000, &#8216;High Salary&#8217;, &#8216;Low Salary&#8217;) AS salary_status FROM employees; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u67e5\u8be2\u4f1a\u8fd4\u56de\u4e00\u4e2a\u540d\u4e3a salary_status \u7684\u65b0\u5217\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u503c\uff1a \u5982\u679c\u5458\u5de5\u7684\u5de5\u8d44\u9ad8\u4e8e 50,000\uff0c\u5219\u4e3a &#8220;High Salary&#8221; \u5426\u5219\uff0c\u4e3a &#8220;Low Salary&#8221; \u5e94\u7528\u573a\u666f NVL2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-4991","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/4991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=4991"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/4991\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=4991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=4991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=4991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}