{"id":5912,"date":"2024-11-14T11:37:35","date_gmt":"2024-11-14T03:37:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5912\/"},"modified":"2024-11-14T11:37:35","modified_gmt":"2024-11-14T03:37:35","slug":"sql%e6%80%8e%e4%b9%88%e6%8a%8a%e5%ad%97%e7%ac%a6%e5%9e%8b%e5%8f%98%e6%88%90%e6%95%b0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5912\/","title":{"rendered":"sql\u600e\u4e48\u628a\u5b57\u7b26\u578b\u53d8\u6210\u6570\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql \u4e2d\uff0c\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u6709\uff1a1. cast \u51fd\u6570\u5f3a\u5236\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff1b2. convert \u51fd\u6570\u7c7b\u4f3c\u4e8e cast \u51fd\u6570\uff1b3. to_number \u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u5b57\u7b26\u578b\u8f6c\u6570\u5b57\uff08oracle\uff09\uff1b4. numeric \u51fd\u6570\u5141\u8bb8\u6307\u5b9a\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\uff08sql server\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322151975181.jpg\" class=\"aligncenter\" title=\"sql\u600e\u4e48\u628a\u5b57\u7b26\u578b\u53d8\u6210\u6570\u503c\u63d2\u56fe\" alt=\"sql\u600e\u4e48\u628a\u5b57\u7b26\u578b\u53d8\u6210\u6570\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c<\/strong><\/p>\n<p>\u5728 SQL \u4e2d\uff0c\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. CAST \u51fd\u6570<\/strong><\/p>\n<p>CAST \u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CAST(expression AS data_type)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u578b\u5217 &#8220;salary&#8221; \u8f6c\u6362\u4e3a\u6570\u5b57\uff1a<\/p>\n<pre>SELECT CAST(salary AS DECIMAL) FROM employees;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. CONVERT \u51fd\u6570<\/strong><\/p>\n<p>CONVERT \u51fd\u6570\u4e0e CAST \u51fd\u6570\u7c7b\u4f3c\uff0c\u7528\u4e8e\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CONVERT(data_type, expression)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u578b\u5217 &#8220;id&#8221; \u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<pre>SELECT CONVERT(INT, id) FROM customers;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. TO_NUMBER \u51fd\u6570\uff08Oracle\uff09<\/strong><\/p>\n<p>TO_NUMBER \u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>TO_NUMBER(expression)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u578b\u5217 &#8220;qty&#8221; \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff1a<\/p>\n<pre>SELECT TO_NUMBER(qty) FROM orders;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. NUMERIC \u51fd\u6570\uff08SQL Server\uff09<\/strong><\/p>\n<p>NUMERIC \u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u5b57\u5e76\u5141\u8bb8\u6307\u5b9a\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>NUMERIC(precision, scale, expression)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u578b\u5217 &#8220;price&#8221; \u8f6c\u6362\u4e3a\u5177\u6709 10 \u4f4d\u7cbe\u5ea6\u548c 2 \u4f4d\u5c0f\u6570\u7684\u6570\u5b57\uff1a<\/p>\n<pre>SELECT NUMERIC(10, 2, price) FROM products;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\uff0c\u8bf7\u786e\u4fdd\u5b57\u7b26\u578b\u6570\u636e\u53ef\u4ee5\u5408\u6cd5\u5730\u8f6c\u6362\u4e3a\u6570\u503c\u3002\u5426\u5219\uff0c\u51fd\u6570\u5c06\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n<li>\u8f6c\u6362\u540e\uff0c\u6570\u636e\u7684\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\u53ef\u80fd\u4e0e\u539f\u59cb\u5b57\u7b26\u578b\u6570\u636e\u4e0d\u540c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u600e\u4e48\u628a\u5b57\u7b26\u578b\u53d8\u6210\u6570\u503c\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>\u5728 sql \u4e2d\uff0c\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u6709\uff1a1. cast \u51fd\u6570\u5f3a\u5236\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff1b2. convert \u51fd\u6570\u7c7b\u4f3c\u4e8e cast \u51fd\u6570\uff1b3. to_number \u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u5b57\u7b26\u578b\u8f6c\u6570\u5b57\uff08oracle\uff09\uff1b4. numeric \u51fd\u6570\u5141\u8bb8\u6307\u5b9a\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\uff08sql server\uff09\u3002 \u5982\u4f55\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c \u5728 SQL \u4e2d\uff0c\u5c06\u5b57\u7b26\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. CAST \u51fd\u6570 CAST \u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a CAST(expression AS data_type) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u578b\u5217 &#8220;salary&#8221; \u8f6c\u6362\u4e3a\u6570\u5b57\uff1a SELECT CAST(salary AS DECIMAL) FROM employees; \u767b\u5f55\u540e\u590d\u5236 2. CONVERT \u51fd\u6570 CONVERT \u51fd\u6570\u4e0e CAST \u51fd\u6570\u7c7b\u4f3c\uff0c\u7528\u4e8e\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a CONVERT(data_type, expression) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u578b\u5217 &#8220;id&#8221; \u8f6c\u6362\u4e3a\u6574\u6570\uff1a SELECT CONVERT(INT, id) FROM customers; [&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-5912","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5912","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=5912"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5912\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}