{"id":138967,"date":"2024-10-31T17:06:28","date_gmt":"2024-10-31T09:06:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/138967\/"},"modified":"2024-10-31T17:06:29","modified_gmt":"2024-10-31T09:06:29","slug":"sql-server%e6%8e%92%e5%ba%8f%e9%81%87%e5%88%b0null%ef%bc%8c%e4%b8%8d%e6%80%95%e4%b8%8d%e6%80%95%ef%bc%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/138967\/","title":{"rendered":"SQL Server\u6392\u5e8f\u9047\u5230NULL\uff0c\u4e0d\u6015\u4e0d\u6015\uff01"},"content":{"rendered":"<h1 id=\"sql-server%e6%8e%92%e5%ba%8f%e9%81%87%e5%88%b0null%ef%bc%8c%e4%b8%8d%e6%80%95%e4%b8%8d%e6%80%95%ef%bc%81-eVdDJVVwgb\">SQL Server\u6392\u5e8f\u9047\u5230NULL\uff0c\u4e0d\u6015\u4e0d\u6015\uff01<\/h1>\n<p>\u5728\u4f7f\u7528SQL Server\u9032\u884c\u6578\u64da\u67e5\u8a62\u6642\uff0c\u6392\u5e8f\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u7136\u800c\uff0c\u7576\u6578\u64da\u4e2d\u5305\u542bNULL\u503c\u6642\uff0c\u6392\u5e8f\u7684\u884c\u70ba\u53ef\u80fd\u6703\u8b93\u4eba\u611f\u5230\u56f0\u60d1\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eSQL Server\u5728\u6392\u5e8f\u6642\u5982\u4f55\u8655\u7406NULL\u503c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u89e3\u6c7a\u65b9\u6848\u548c\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"null%e5%80%bc%e7%9a%84%e7%89%b9%e6%80%a7-eVdDJVVwgb\">NULL\u503c\u7684\u7279\u6027<\/h2>\n<p>\u5728SQL\u4e2d\uff0cNULL\u8868\u793a\u7f3a\u5931\u6216\u672a\u77e5\u7684\u6578\u64da\u3002\u9019\u610f\u5473\u8457NULL\u4e0d\u7b49\u65bc\u4efb\u4f55\u503c\uff0c\u5305\u62ec\u81ea\u8eab\u3002\u56e0\u6b64\uff0c\u5728\u9032\u884c\u6392\u5e8f\u6642\uff0cSQL Server\u6703\u5c07NULL\u8996\u70ba\u4e00\u500b\u7279\u6b8a\u7684\u503c\u3002\u6839\u64daSQL Server\u7684\u9ed8\u8a8d\u884c\u70ba\uff0cNULL\u503c\u5728\u6392\u5e8f\u6642\u6703\u88ab\u653e\u7f6e\u5728\u7d50\u679c\u96c6\u7684\u6700\u524d\u9762\uff08\u5347\u5e8f\u6392\u5e8f\uff09\u6216\u6700\u5f8c\u9762\uff08\u964d\u5e8f\u6392\u5e8f\uff09\u3002<\/p>\n<h2 id=\"%e6%8e%92%e5%ba%8f%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-eVdDJVVwgb\">\u6392\u5e8f\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0c\u4f7f\u7528ORDER BY\u5b50\u53e5\u4f86\u9032\u884c\u6392\u5e8f\u3002\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2\nFROM table_name\nORDER BY column1 [ASC|DESC];\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cASC\u8868\u793a\u5347\u5e8f\uff0cDESC\u8868\u793a\u964d\u5e8f\u3002\u5982\u679ccolumn1\u4e2d\u5305\u542bNULL\u503c\uff0c\u5247\u5728\u5347\u5e8f\u6392\u5e8f\u6642\uff0cNULL\u5c07\u6392\u5728\u6700\u524d\u9762\uff1b\u5728\u964d\u5e8f\u6392\u5e8f\u6642\uff0cNULL\u5c07\u6392\u5728\u6700\u5f8c\u9762\u3002<\/p>\n<h2 id=\"%e8%87%aa%e5%ae%9a%e7%be%a9null%e7%9a%84%e6%8e%92%e5%ba%8f%e4%bd%8d%e7%bd%ae-eVdDJVVwgb\">\u81ea\u5b9a\u7fa9NULL\u7684\u6392\u5e8f\u4f4d\u7f6e<\/h2>\n<p>\u5982\u679c\u5e0c\u671b\u81ea\u5b9a\u7fa9NULL\u503c\u7684\u6392\u5e8f\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528CASE\u8a9e\u53e5\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8a62\u5c07NULL\u503c\u653e\u5728\u6700\u5f8c\uff1a<\/p>\n<pre><code>SELECT column1\nFROM table_name\nORDER BY \n    CASE \n        WHEN column1 IS NULL THEN 1 \n        ELSE 0 \n    END, \n    column1 ASC;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u67e5\u8a62\u4e2d\uff0cCASE\u8a9e\u53e5\u70baNULL\u503c\u5206\u914d\u4e86\u4e00\u500b\u8f03\u9ad8\u7684\u6392\u5e8f\u6b0a\u91cd\uff081\uff09\uff0c\u800c\u975eNULL\u503c\u5247\u70ba0\u3002\u9019\u6a23\uff0c\u6240\u6709\u975eNULL\u503c\u5c07\u9996\u5148\u986f\u793a\uff0c\u7136\u5f8c\u662fNULL\u503c\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8isnull%e5%92%8ccoalesce%e5%87%bd%e6%95%b8-eVdDJVVwgb\">\u4f7f\u7528ISNULL\u548cCOALESCE\u51fd\u6578<\/h2>\n<p>\u53e6\u4e00\u7a2e\u8655\u7406NULL\u503c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528ISNULL\u6216COALESCE\u51fd\u6578\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5c07NULL\u503c\u66ff\u63db\u70ba\u5176\u4ed6\u503c\uff0c\u5f9e\u800c\u5f71\u97ff\u6392\u5e8f\u7d50\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT column1\nFROM table_name\nORDER BY ISNULL(column1, '\u66ff\u4ee3\u503c') ASC;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709NULL\u503c\u5c07\u88ab\u66ff\u63db\u70ba\u201c\u66ff\u4ee3\u503c\u201d\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5b83\u5011\u5728\u6392\u5e8f\u6642\u4e0d\u6703\u5f71\u97ff\u7d50\u679c\u7684\u9806\u5e8f\u3002<\/p>\n<h2 id=\"%e7%af%84%e4%be%8b%e5%88%86%e6%9e%90-eVdDJVVwgb\">\u7bc4\u4f8b\u5206\u6790<\/h2>\n<p>\u5047\u8a2d\u6709\u4e00\u500b\u540d\u70baEmployees\u7684\u8868\u683c\uff0c\u5305\u542b\u4ee5\u4e0b\u6578\u64da\uff1a<\/p>\n<pre><code>+----+----------+\n| ID | Name     |\n+----+----------+\n| 1  | Alice    |\n| 2  | Bob      |\n| 3  | NULL     |\n| 4  | Charlie  |\n| 5  | NULL     |\n+----+----------+\n<\/code><\/pre>\n<p>\u5982\u679c\u6211\u5011\u57f7\u884c\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT Name\nFROM Employees\nORDER BY Name ASC;\n<\/code><\/pre>\n<p>\u7d50\u679c\u5c07\u662f\uff1a<\/p>\n<pre><code>+----------+\n| Name     |\n+----------+\n| NULL     |\n| Alice    |\n| Bob      |\n| Charlie  |\n+----------+\n<\/code><\/pre>\n<p>\u800c\u5982\u679c\u6211\u5011\u4f7f\u7528\u81ea\u5b9a\u7fa9\u6392\u5e8f\uff1a<\/p>\n<pre><code>SELECT Name\nFROM Employees\nORDER BY \n    CASE \n        WHEN Name IS NULL THEN 1 \n        ELSE 0 \n    END, \n    Name ASC;\n<\/code><\/pre>\n<p>\u7d50\u679c\u5c07\u662f\uff1a<\/p>\n<pre><code>+----------+\n| Name     |\n+----------+\n| Alice    |\n| Bob      |\n| Charlie  |\n| NULL     |\n+----------+\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-eVdDJVVwgb\">\u7e3d\u7d50<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0cNULL\u503c\u7684\u6392\u5e8f\u884c\u70ba\u53ef\u80fd\u6703\u5f71\u97ff\u67e5\u8a62\u7d50\u679c\u7684\u53ef\u8b80\u6027\u548c\u908f\u8f2f\u6027\u3002\u901a\u904e\u4f7f\u7528CASE\u8a9e\u53e5\u3001ISNULL\u6216COALESCE\u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u9748\u6d3b\u5730\u63a7\u5236NULL\u503c\u7684\u6392\u5e8f\u4f4d\u7f6e\uff0c\u5f9e\u800c\u9054\u5230\u66f4\u7b26\u5408\u9700\u6c42\u7684\u7d50\u679c\u3002\u4e86\u89e3\u9019\u4e9b\u6280\u5de7\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5728\u8655\u7406\u6578\u64da\u6642\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728SQL Server\u4e2d\u5904\u7406\u6392\u5e8f\u65f6\u9047\u5230NULL\u503c\u7684\u60c5\u51b5\uff0c\u8f7b\u677e\u5e94\u5bf9\uff0c\u786e\u4fdd\u6570\u636e\u6392\u5e8f\u51c6\u786e\u65e0\u8bef\uff01<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-138967","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=138967"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138967\/revisions"}],"predecessor-version":[{"id":138968,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138967\/revisions\/138968"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=138967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=138967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=138967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}