{"id":139690,"date":"2024-10-31T19:52:22","date_gmt":"2024-10-31T11:52:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/139690\/"},"modified":"2024-10-31T19:52:23","modified_gmt":"2024-10-31T11:52:23","slug":"sql-server%e6%95%b8%e6%93%9a%e9%a1%9e%e5%9e%8b%e8%bd%89%e6%8f%9b%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/139690\/","title":{"rendered":"Sql Server\u6578\u64da\u985e\u578b\u8f49\u63db\u51fd\u6578"},"content":{"rendered":"<h1 id=\"sql-server%e6%95%b8%e6%93%9a%e9%a1%9e%e5%9e%8b%e8%bd%89%e6%8f%9b%e5%87%bd%e6%95%b8-xrLxiQsrHW\">SQL Server\u6578\u64da\u985e\u578b\u8f49\u63db\u51fd\u6578<\/h1>\n<p>\u5728SQL Server\u4e2d\uff0c\u6578\u64da\u985e\u578b\u8f49\u63db\u662f\u6578\u64da\u8655\u7406\u904e\u7a0b\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u6b65\u9a5f\u3002\u7576\u6211\u5011\u9700\u8981\u5c07\u4e00\u7a2e\u6578\u64da\u985e\u578b\u8f49\u63db\u70ba\u53e6\u4e00\u7a2e\u6642\uff0c\u4f7f\u7528\u9069\u7576\u7684\u8f49\u63db\u51fd\u6578\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u6e96\u78ba\u6027\u548c\u5b8c\u6574\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0eSQL Server\u4e2d\u7684\u6578\u64da\u985e\u578b\u8f49\u63db\u51fd\u6578\uff0c\u5305\u62ecCAST\u548cCONVERT\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u61c9\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e9%a1%9e%e5%9e%8b%e8%bd%89%e6%8f%9b%e7%9a%84%e5%bf%85%e8%a6%81%e6%80%a7-xrLxiQsrHW\">\u6578\u64da\u985e\u578b\u8f49\u63db\u7684\u5fc5\u8981\u6027<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\uff0c\u6578\u64da\u985e\u578b\u8f49\u63db\u662f\u5e38\u898b\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u7576\u5f9e\u6578\u5b57\u985e\u578b\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u985e\u578b\u6642\uff0c\u6216\u8005\u5728\u9032\u884c\u65e5\u671f\u8a08\u7b97\u6642\uff0c\u53ef\u80fd\u9700\u8981\u5c07\u65e5\u671f\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u9019\u4e9b\u8f49\u63db\u4e0d\u50c5\u6709\u52a9\u65bc\u6578\u64da\u7684\u986f\u793a\uff0c\u9084\u80fd\u78ba\u4fdd\u5728\u9032\u884c\u6578\u64da\u6bd4\u8f03\u548c\u8a08\u7b97\u6642\u4e0d\u6703\u51fa\u73fe\u932f\u8aa4\u3002<\/p>\n<h2 id=\"cast%e5%87%bd%e6%95%b8-xrLxiQsrHW\">CAST\u51fd\u6578<\/h2>\n<p>CAST\u51fd\u6578\u662fSQL Server\u4e2d\u7528\u65bc\u6578\u64da\u985e\u578b\u8f49\u63db\u7684\u57fa\u672c\u51fd\u6578\u3002\u5176\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CAST(expression AS data_type)<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>expression<\/code>\u662f\u8981\u8f49\u63db\u7684\u6578\u64da\uff0c<code>data_type<\/code>\u662f\u76ee\u6a19\u6578\u64da\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528CAST\u51fd\u6578\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT CAST('2023-10-01' AS DATE) AS ConvertedDate;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5c07\u5b57\u7b26\u4e32&#8217;2023-10-01&#8217;\u8f49\u63db\u70baDATE\u985e\u578b\uff0c\u7d50\u679c\u5c07\u986f\u793a\u70ba\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<h2 id=\"convert%e5%87%bd%e6%95%b8-xrLxiQsrHW\">CONVERT\u51fd\u6578<\/h2>\n<p>CONVERT\u51fd\u6578\u63d0\u4f9b\u4e86\u66f4\u9748\u6d3b\u7684\u6578\u64da\u985e\u578b\u8f49\u63db\u9078\u9805\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u65e5\u671f\u548c\u6642\u9593\u683c\u5f0f\u6642\u3002\u5176\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CONVERT(data_type, expression [, style])<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c<code>style<\/code>\u662f\u4e00\u500b\u53ef\u9078\u7684\u53c3\u6578\uff0c\u7528\u65bc\u6307\u5b9a\u8f49\u63db\u7684\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528CONVERT\u51fd\u6578\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS FormattedDate;<\/code><\/pre>\n<p>\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5c07\u7576\u524d\u65e5\u671f\u8f49\u63db\u70baVARCHAR\u985e\u578b\uff0c\u4e26\u4f7f\u7528101\u683c\u5f0f\uff08MM\/DD\/YYYY\uff09\u986f\u793a\u7d50\u679c\u3002<\/p>\n<h2 id=\"cast%e8%88%87convert%e7%9a%84%e5%8d%80%e5%88%a5-xrLxiQsrHW\">CAST\u8207CONVERT\u7684\u5340\u5225<\/h2>\n<p>\u96d6\u7136CAST\u548cCONVERT\u90fd\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u985e\u578b\u8f49\u63db\uff0c\u4f46\u5b83\u5011\u4e4b\u9593\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u5340\u5225\uff1a<\/p>\n<ul>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>CONVERT\u51fd\u6578\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u683c\u5f0f\u9078\u9805\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u65e5\u671f\u548c\u6642\u9593\u6642\u3002<\/li>\n<li><strong>\u53ef\u8b80\u6027\uff1a<\/strong>CAST\u51fd\u6578\u7684\u8a9e\u6cd5\u8f03\u70ba\u7c21\u55ae\uff0c\u9069\u5408\u65bc\u57fa\u672c\u7684\u6578\u64da\u985e\u578b\u8f49\u63db\u3002<\/li>\n<li><strong>\u6027\u80fd\uff1a<\/strong>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0cCAST\u53ef\u80fd\u6bd4CONVERT\u66f4\u5feb\uff0c\u4f46\u5177\u9ad4\u6027\u80fd\u53d6\u6c7a\u65bc\u6578\u64da\u7684\u5927\u5c0f\u548c\u985e\u578b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-xrLxiQsrHW\">\u61c9\u7528\u5834\u666f<\/h2>\n<p>\u6578\u64da\u985e\u578b\u8f49\u63db\u51fd\u6578\u5728\u591a\u7a2e\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u5728\u5831\u544a\u4e2d\u9700\u8981\u5c07\u6578\u5b57\u683c\u5f0f\u5316\u70ba\u8ca8\u5e63\u683c\u5f0f\u3002<\/li>\n<li>\u5728\u67e5\u8a62\u4e2d\u9700\u8981\u5c07\u65e5\u671f\u8f49\u63db\u70ba\u7279\u5b9a\u683c\u5f0f\u4ee5\u4fbf\u65bc\u986f\u793a\u3002<\/li>\n<li>\u5728\u6578\u64da\u6e05\u7406\u904e\u7a0b\u4e2d\uff0c\u9700\u8981\u5c07\u4e0d\u4e00\u81f4\u7684\u6578\u64da\u985e\u578b\u7d71\u4e00\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-xrLxiQsrHW\">\u7e3d\u7d50<\/h2>\n<p>SQL Server\u4e2d\u7684\u6578\u64da\u985e\u578b\u8f49\u63db\u51fd\u6578\u5982CAST\u548cCONVERT\u662f\u6578\u64da\u8655\u7406\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u5b83\u5011\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u78ba\u4fdd\u6578\u64da\u7684\u6e96\u78ba\u6027\u3002\u5728\u9032\u884c\u6578\u64da\u5eab\u958b\u767c\u548c\u7ba1\u7406\u6642\uff0c\u719f\u6089\u9019\u4e9b\u51fd\u6578\u7684\u4f7f\u7528\u5c07\u5927\u5927\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u3002\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u652f\u6301\u60a8\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3SQL Server\u6578\u64da\u985e\u578b\u8f49\u63db\u51fd\u6578\uff0c\u638c\u63e1\u5982\u4f55\u5728\u6578\u64da\u5eab\u4e2d\u6709\u6548\u5730\u8f49\u63db\u548c\u8655\u7406\u4e0d\u540c\u6578\u64da\u985e\u578b\u3002<\/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-139690","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139690","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=139690"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139690\/revisions"}],"predecessor-version":[{"id":139691,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139690\/revisions\/139691"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=139690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=139690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=139690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}