{"id":1747,"date":"2025-02-16T09:16:02","date_gmt":"2025-02-16T01:16:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1747"},"modified":"2025-02-16T09:16:02","modified_gmt":"2025-02-16T01:16:02","slug":"sql-format-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-format-%e5%87%bd%e6%95%b0\/","title":{"rendered":"SQL FORMAT()\u00a0\u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>FORMAT()<\/code> \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u6570\u5b57\u6216\u65e5\u671f\u6309\u7167\u6307\u5b9a\u7684\u683c\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u8fd9\u4e2a\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u8f93\u51fa\u7684\u683c\u5f0f\u5316\uff0c\u5c24\u5176\u662f\u5728\u62a5\u544a\u6216\u5c55\u793a\u6570\u636e\u65f6\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u663e\u793a\u6216\u9605\u8bfb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>FORMAT()<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>\u683c\u5f0f\u5316\u6570\u5b57<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>FORMAT(number, decimal_places, locale);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>number<\/code>\uff1a\u8981\u683c\u5f0f\u5316\u7684\u6570\u5b57\u3002<\/li>\n\n\n\n<li><code>decimal_places<\/code>\uff1a\u8981\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/li>\n\n\n\n<li><code>locale<\/code>\uff08\u53ef\u9009\uff09\uff1a\u8868\u793a\u5730\u533a\u7684\u53c2\u6570\uff0c\u7528\u4e8e\u786e\u5b9a\u6570\u5b57\u683c\u5f0f\uff08\u4f8b\u5982\u5343\u4f4d\u5206\u9694\u7b26\u7684\u7b26\u53f7\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>\u683c\u5f0f\u5316\u65e5\u671f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u6570\u636e\u5e93\uff08\u5982 MySQL\uff09\u4e2d\uff0c<code>FORMAT()<\/code> \u4e5f\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FORMAT(date, format);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>date<\/code>\uff1a\u8981\u683c\u5f0f\u5316\u7684\u65e5\u671f\u3002<\/li>\n\n\n\n<li><code>format<\/code>\uff1a\u65e5\u671f\u7684\u683c\u5f0f\uff08\u4f7f\u7528\u7279\u5b9a\u7684\u683c\u5f0f\u5316\u7b26\u53f7\uff0c\u5982 <code>'%Y-%m-%d'<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>FORMAT()<\/code> \u7684\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u6570\u5b57\u683c\u5f0f\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>sales<\/code> \u7684\u8868\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a <code>sale_amount<\/code> \u5217\u8868\u793a\u9500\u552e\u91d1\u989d\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>FORMAT()<\/code> \u6765\u5c06 <code>sale_amount<\/code> \u683c\u5f0f\u5316\u4e3a\u5e26\u6709\u4e24\u4f4d\u5c0f\u6570\u5e76\u4f7f\u7528\u5343\u4f4d\u5206\u9694\u7b26\u7684\u6570\u5b57\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT sale_id, FORMAT(sale_amount, 2) AS formatted_amount\nFROM sales;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>sale_id<\/th><th>formatted_amount<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>1,234.56<\/td><\/tr><tr><td>2<\/td><td>789.12<\/td><\/tr><tr><td>3<\/td><td>45.67<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>FORMAT(sale_amount, 2)<\/code> \u5c06 <code>sale_amount<\/code> \u56db\u820d\u4e94\u5165\u5230\u4e24\u4f4d\u5c0f\u6570\uff0c\u5e76\u4f7f\u7528\u5343\u4f4d\u5206\u9694\u7b26\u683c\u5f0f\u5316\u6570\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u6307\u5b9a\u5730\u533a\u683c\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>FORMAT()<\/code> \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7 <code>locale<\/code> \u53c2\u6570\u6765\u6839\u636e\u7279\u5b9a\u5730\u533a\u7684\u6570\u5b57\u683c\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>en_US<\/code> \u6765\u6307\u5b9a\u7f8e\u56fd\u7684\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT sale_id, FORMAT(sale_amount, 2, 'en_US') AS formatted_amount\nFROM sales;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u4f7f\u6570\u5b57\u683c\u5f0f\u4f7f\u7528\u7f8e\u56fd\u7684\u6807\u51c6\u6570\u5b57\u683c\u5f0f\uff08\u5373\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5343\u4f4d\u5206\u9694\u7b26\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>\u65e5\u671f\u683c\u5f0f\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 MySQL \u4e2d\uff0c<code>FORMAT()<\/code> \u4e5f\u53ef\u4ee5\u7528\u6765\u683c\u5f0f\u5316\u65e5\u671f\u3002\u4f8b\u5982\uff0c\u683c\u5f0f\u5316\u65e5\u671f\u4e3a <code>YYYY-MM-DD<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT FORMAT(order_date, '%Y-%m-%d') AS formatted_date\nFROM orders;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>formatted_date<\/th><\/tr><\/thead><tbody><tr><td>2025-02-16<\/td><\/tr><tr><td>2025-02-15<\/td><\/tr><tr><td>2025-02-14<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>FORMAT()<\/code> \u51fd\u6570\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u6570\u636e\u5e93\u652f\u6301<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e76\u4e0d\u662f\u6240\u6709\u7684\u6570\u636e\u5e93\u90fd\u652f\u6301 <code>FORMAT()<\/code> \u51fd\u6570\u3002<code>FORMAT()<\/code> \u51fd\u6570\u662f MySQL \u548c SQL Server \u4e2d\u7684\u4e00\u4e2a\u5e38\u7528\u51fd\u6570\uff0c\u4f46\u5176\u4ed6\u6570\u636e\u5e93\uff08\u5982 Oracle \u6216 PostgreSQL\uff09\u53ef\u80fd\u4e0d\u652f\u6301\u8be5\u51fd\u6570\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u4e0d\u652f\u6301 <code>FORMAT()<\/code> \u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <strong>PostgreSQL<\/strong> \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>TO_CHAR()<\/code> \u6765\u683c\u5f0f\u5316\u6570\u5b57\u548c\u65e5\u671f\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT TO_CHAR(sale_amount, 'FM999,999,999.00') FROM sales;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <strong>Oracle<\/strong> \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>TO_CHAR()<\/code> \u8fdb\u884c\u683c\u5f0f\u5316\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT TO_CHAR(sale_amount, '999,999,999.00') FROM sales;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong>\u5c0f\u6570\u4f4d\u6570\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u59cb\u7ec8\u663e\u793a\u56fa\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\uff08\u5373\u4f7f\u6570\u5b57\u4e3a\u6574\u6570\uff09\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>FORMAT()<\/code> \u6765\u786e\u4fdd\u6570\u5b57\u663e\u793a\u4e3a\u56fa\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT FORMAT(sale_amount, 2) FROM sales;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5373\u4f7f <code>sale_amount<\/code> \u662f\u6574\u6570\uff0c\u8fd9\u4e5f\u4f1a\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u5e26\u6709\u4e24\u4f4d\u5c0f\u6570\u7684\u6570\u5b57\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.3 <strong>\u5730\u533a\u683c\u5f0f\u5dee\u5f02<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u5730\u533a\u7684\u6570\u5b57\u683c\u5f0f\u53ef\u80fd\u4e0d\u540c\uff0c\u5c24\u5176\u662f\u5343\u4f4d\u5206\u9694\u7b26\u548c\u5c0f\u6570\u70b9\u7b26\u53f7\u7684\u4f7f\u7528\u3002\u786e\u4fdd\u9009\u62e9\u6b63\u786e\u7684 <code>locale<\/code> \u53c2\u6570\u6765\u5904\u7406\u533a\u57df\u5dee\u5f02\u3002\u4f8b\u5982\uff0c\u5728\u6b27\u6d32\u4e00\u4e9b\u56fd\u5bb6\uff0c\u6570\u5b57\u4f7f\u7528\u7a7a\u683c\u6216\u70b9\u4f5c\u4e3a\u5343\u4f4d\u5206\u9694\u7b26\uff0c\u800c\u4e0d\u662f\u9017\u53f7\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5730\u533a\u4ee3\u7801\u6765\u5904\u7406\u8fd9\u4e9b\u5dee\u5f02\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.4 <strong><code>NULL<\/code> \u503c\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>FORMAT()<\/code> \u51fd\u6570\u5904\u7406 <code>NULL<\/code> \u503c\u65f6\uff0c\u5c06\u8fd4\u56de <code>NULL<\/code>\uff0c\u800c\u4e0d\u662f\u62a5\u9519\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5bf9 <code>NULL<\/code> \u503c\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COALESCE()<\/code> \u6216 <code>IFNULL()<\/code> \u7b49\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT FORMAT(COALESCE(sale_amount, 0), 2) FROM sales;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>FORMAT()<\/code> \u7528\u4e8e\u5c06\u6570\u5b57\u548c\u65e5\u671f\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u683c\u5f0f\u3002<\/li>\n\n\n\n<li>\u5b83\u652f\u6301\u6570\u5b57\u683c\u5f0f\u5316\uff08\u5305\u62ec\u5343\u4f4d\u5206\u9694\u7b26\u548c\u5c0f\u6570\u4f4d\u6570\uff09\u4ee5\u53ca\u65e5\u671f\u683c\u5f0f\u5316\u3002<\/li>\n\n\n\n<li><code>FORMAT()<\/code> \u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u6570\u636e\u5e93\uff0c\u5982\u679c\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u5e93\u63d0\u4f9b\u7684\u683c\u5f0f\u5316\u51fd\u6570\uff0c\u5982 <code>TO_CHAR()<\/code>\u3002<\/li>\n\n\n\n<li>\u5728\u4f7f\u7528\u65f6\u8981\u6ce8\u610f\u9009\u62e9\u5408\u9002\u7684\u5730\u533a\u4ee3\u7801\uff08<code>locale<\/code>\uff09\u4ee5\u786e\u4fdd\u6570\u5b57\u683c\u5f0f\u7b26\u5408\u9884\u671f\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>FORMAT() \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u6570\u5b57\u6216\u65e5\u671f\u6309\u7167\u6307\u5b9a\u7684\u683c\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u8fd9\u4e2a\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u8f93\u51fa\u7684\u683c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1747","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1747"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1747\/revisions"}],"predecessor-version":[{"id":1748,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1747\/revisions\/1748"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}