{"id":66107,"date":"2024-10-14T14:32:26","date_gmt":"2024-10-14T06:32:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/66107\/"},"modified":"2024-10-14T14:32:26","modified_gmt":"2024-10-14T06:32:26","slug":"mssql%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%8a%9f%e8%83%bd%e7%9a%84%e6%8a%80%e5%b7%a7%ef%bc%88mssql%e6%a0%bc%e5%bc%8f%e5%8c%96%e6%95%b8%e6%93%9a%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/66107\/","title":{"rendered":"MSSQL\u5be6\u73fe\u6578\u64da\u683c\u5f0f\u5316\u529f\u80fd\u7684\u6280\u5de7\uff08mssql\u683c\u5f0f\u5316\u6578\u64da\uff09"},"content":{"rendered":"<h1 id=\"mssql%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%8a%9f%e8%83%bd%e7%9a%84%e6%8a%80%e5%b7%a7%ef%bc%88mssql%e6%a0%bc%e5%bc%8f%e5%8c%96%e6%95%b8%e6%93%9a%ef%bc%89-XpAZSzndgJ\">MSSQL\u5be6\u73fe\u6578\u64da\u683c\u5f0f\u5316\u529f\u80fd\u7684\u6280\u5de7\uff08mssql\u683c\u5f0f\u5316\u6578\u64da\uff09<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u6578\u64da\u683c\u5f0f\u5316\u662f\u4e00\u500b\u91cd\u8981\u7684\u904e\u7a0b\uff0c\u7279\u5225\u662f\u5728\u4f7f\u7528Microsoft SQL Server\uff08MSSQL\uff09\u6642\u3002\u6578\u64da\u683c\u5f0f\u5316\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u7406\u89e3\u548c\u5206\u6790\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5728MSSQL\u4e2d\u5be6\u73fe\u6578\u64da\u683c\u5f0f\u5316\u7684\u5e7e\u7a2e\u6280\u5de7\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8cast%e5%92%8cconvert%e5%87%bd%e6%95%b8-XpAZSzndgJ\">1. \u4f7f\u7528CAST\u548cCONVERT\u51fd\u6578<\/h2>\n<p>MSSQL\u63d0\u4f9b\u4e86\u5169\u500b\u4e3b\u8981\u7684\u51fd\u6578\u4f86\u9032\u884c\u6578\u64da\u985e\u578b\u7684\u8f49\u63db\uff1a<code>CAST<\/code>\u548c<code>CONVERT<\/code>\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u5c07\u6578\u64da\u8f49\u63db\u70ba\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u5c07\u65e5\u671f\u8f49\u63db\u70ba\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<h3 id=\"1-1-cast%e5%87%bd%e6%95%b8-XpAZSzndgJ\">1.1 CAST\u51fd\u6578<\/h3>\n<p><code>CAST<\/code>\u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CAST(expression AS data_type)<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5c07\u4e00\u500b\u65e5\u671f\u683c\u5f0f\u5316\u70ba\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>SELECT CAST(GETDATE() AS VARCHAR(10)) AS FormattedDate;<\/code><\/pre>\n<h3 id=\"1-2-convert%e5%87%bd%e6%95%b8-XpAZSzndgJ\">1.2 CONVERT\u51fd\u6578<\/h3>\n<p><code>CONVERT<\/code>\u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CONVERT(data_type, expression, style)<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>style<\/code>\u53c3\u6578\u53ef\u4ee5\u7528\u4f86\u6307\u5b9a\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5c07\u65e5\u671f\u683c\u5f0f\u5316\u70ba\u201cYYYY-MM-DD\u201d\u683c\u5f0f\uff1a<\/p>\n<pre><code>SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS FormattedDate;<\/code><\/pre>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8format%e5%87%bd%e6%95%b8-XpAZSzndgJ\">2. \u4f7f\u7528FORMAT\u51fd\u6578<\/h2>\n<p>\u5728SQL Server 2012\u53ca\u4ee5\u5f8c\u7684\u7248\u672c\u4e2d\uff0c<code>FORMAT<\/code>\u51fd\u6578\u63d0\u4f9b\u4e86\u4e00\u7a2e\u66f4\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u683c\u5f0f\u5316\u6578\u64da\u3002\u5176\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>FORMAT(value, format_string, culture)<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5c07\u6578\u5b57\u683c\u5f0f\u5316\u70ba\u8ca8\u5e63\u5f62\u5f0f\uff1a<\/p>\n<pre><code>SELECT FORMAT(12345.6789, 'C', 'en-US') AS FormattedCurrency;<\/code><\/pre>\n<h2 id=\"3-%e6%97%a5%e6%9c%9f%e5%92%8c%e6%99%82%e9%96%93%e7%9a%84%e6%a0%bc%e5%bc%8f%e5%8c%96-XpAZSzndgJ\">3. \u65e5\u671f\u548c\u6642\u9593\u7684\u683c\u5f0f\u5316<\/h2>\n<p>\u65e5\u671f\u548c\u6642\u9593\u7684\u683c\u5f0f\u5316\u5728\u6578\u64da\u5831\u544a\u4e2d\u5c24\u70ba\u91cd\u8981\u3002\u4f7f\u7528<code>FORMAT<\/code>\u51fd\u6578\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u65e5\u671f\u683c\u5f0f\u5316\u70ba\u6240\u9700\u7684\u683c\u5f0f\uff1a<\/p>\n<pre><code>SELECT FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm:ss') AS FormattedDateTime;<\/code><\/pre>\n<h2 id=\"4-%e6%95%b8%e5%ad%97%e7%9a%84%e6%a0%bc%e5%bc%8f%e5%8c%96-XpAZSzndgJ\">4. \u6578\u5b57\u7684\u683c\u5f0f\u5316<\/h2>\n<p>\u5c0d\u65bc\u6578\u5b57\u7684\u683c\u5f0f\u5316\uff0c<code>FORMAT<\/code>\u51fd\u6578\u540c\u6a23\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u5c07\u6578\u5b57\u683c\u5f0f\u5316\u70ba\u767e\u5206\u6bd4\uff1a<\/p>\n<pre><code>SELECT FORMAT(0.1234, 'P') AS FormattedPercentage;<\/code><\/pre>\n<h2 id=\"5-%e4%bd%bf%e7%94%a8case%e8%aa%9e%e5%8f%a5%e9%80%b2%e8%a1%8c%e6%a2%9d%e4%bb%b6%e6%a0%bc%e5%bc%8f%e5%8c%96-XpAZSzndgJ\">5. \u4f7f\u7528CASE\u8a9e\u53e5\u9032\u884c\u689d\u4ef6\u683c\u5f0f\u5316<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u64da\u7279\u5b9a\u689d\u4ef6\u4f86\u683c\u5f0f\u5316\u6578\u64da\u3002\u9019\u6642\u53ef\u4ee5\u4f7f\u7528<code>CASE<\/code>\u8a9e\u53e5\u3002\u4f8b\u5982\uff0c\u6839\u64da\u5206\u6578\u4f86\u986f\u793a\u7b49\u7d1a\uff1a<\/p>\n<pre><code>SELECT \n    Score,\n    CASE \n        WHEN Score &gt;= 90 THEN 'A'\n        WHEN Score &gt;= 80 THEN 'B'\n        WHEN Score &gt;= 70 THEN 'C'\n        ELSE 'D'\n    END AS Grade\nFROM Students;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XpAZSzndgJ\">\u7e3d\u7d50<\/h2>\n<p>\u5728MSSQL\u4e2d\uff0c\u6578\u64da\u683c\u5f0f\u5316\u662f\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u7406\u89e3\u548c\u5206\u6790\u6578\u64da\u3002\u901a\u904e\u4f7f\u7528<code>CAST<\/code>\u3001<code>CONVERT<\/code>\u3001<code>FORMAT<\/code>\u51fd\u6578\u4ee5\u53ca<code>CASE<\/code>\u8a9e\u53e5\uff0c\u60a8\u53ef\u4ee5\u9748\u6d3b\u5730\u683c\u5f0f\u5316\u5404\u7a2e\u6578\u64da\u985e\u578b\u3002\u9019\u4e9b\u6280\u5de7\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u589e\u5f37\u5831\u544a\u7684\u5c08\u696d\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u6578\u64da\u5eab\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MSSQL\u6578\u64da\u683c\u5f0f\u5316\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u53ef\u8b80\u6027\u8207\u7ba1\u7406\u6548\u7387\uff0c\u8b93\u60a8\u7684\u6578\u64da\u8655\u7406\u66f4\u7c21\u4fbf\u3001\u76f4\u89c0\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-66107","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66107","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=66107"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66107\/revisions"}],"predecessor-version":[{"id":66108,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66107\/revisions\/66108"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=66107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=66107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=66107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}