{"id":64282,"date":"2024-10-14T11:14:18","date_gmt":"2024-10-14T03:14:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/64282\/"},"modified":"2024-10-14T11:14:18","modified_gmt":"2024-10-14T03:14:18","slug":"ms-sql-server%e6%97%a5%e6%9c%9f%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8%e7%9a%84%e6%8f%8f%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/64282\/","title":{"rendered":"ms SQL Server\u65e5\u671f\u6642\u9593\u51fd\u6578\u7684\u63cf\u8ff0"},"content":{"rendered":"<h1 id=\"ms-sql-server-%e6%97%a5%e6%9c%9f%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8%e7%9a%84%e6%8f%8f%e8%bf%b0-pikAleHVxQ\">MS SQL Server \u65e5\u671f\u6642\u9593\u51fd\u6578\u7684\u63cf\u8ff0<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u65e5\u671f\u548c\u6642\u9593\u7684\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u662f\u5728\u9032\u884c\u6578\u64da\u5206\u6790\u548c\u5831\u544a\u6642\u3002MS SQL Server \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65e5\u671f\u6642\u9593\u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u6709\u6548\u5730\u8655\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u6642\u9593\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u65e5\u671f\u6642\u9593\u51fd\u6578\u53ca\u5176\u7528\u6cd5\u3002<\/p>\n<h2 id=\"1-getdate-pikAleHVxQ\">1. GETDATE()<\/h2>\n<p>GETDATE() \u51fd\u6578\u7528\u65bc\u7372\u53d6\u7576\u524d\u7684\u7cfb\u7d71\u65e5\u671f\u548c\u6642\u9593\u3002\u9019\u662f\u4e00\u500b\u975e\u5e38\u5e38\u7528\u7684\u51fd\u6578\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u8a18\u9304\u6578\u64da\u63d2\u5165\u6216\u66f4\u65b0\u6642\u9593\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<pre><code>SELECT GETDATE() AS CurrentDateTime;<\/code><\/pre>\n<h2 id=\"2-dateadd-pikAleHVxQ\">2. DATEADD()<\/h2>\n<p>DATEADD() \u51fd\u6578\u7528\u65bc\u5728\u6307\u5b9a\u7684\u65e5\u671f\u4e0a\u6dfb\u52a0\u4e00\u6bb5\u6642\u9593\u3002\u9019\u500b\u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>DATEADD(datepart, number, date)<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cdatepart \u53ef\u4ee5\u662f\u5e74\u3001\u6708\u3001\u65e5\u7b49\uff0cnumber \u662f\u8981\u6dfb\u52a0\u7684\u6578\u91cf\uff0cdate \u662f\u57fa\u6e96\u65e5\u671f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5728\u7576\u524d\u65e5\u671f\u4e0a\u6dfb\u52a0 10 \u5929\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u53e5\uff1a<\/p>\n<pre><code>SELECT DATEADD(DAY, 10, GETDATE()) AS NewDate;<\/code><\/pre>\n<h2 id=\"3-datediff-pikAleHVxQ\">3. DATEDIFF()<\/h2>\n<p>DATEDIFF() \u51fd\u6578\u7528\u65bc\u8a08\u7b97\u5169\u500b\u65e5\u671f\u4e4b\u9593\u7684\u5dee\u7570\u3002\u5176\u8a9e\u6cd5\u70ba\uff1a<\/p>\n<pre><code>DATEDIFF(datepart, startdate, enddate)<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u8a08\u7b97\u4eca\u5929\u8207\u4e00\u500b\u7279\u5b9a\u65e5\u671f\u4e4b\u9593\u7684\u5929\u6578\u5dee\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT DATEDIFF(DAY, '2023-01-01', GETDATE()) AS DaysDifference;<\/code><\/pre>\n<h2 id=\"4-format-pikAleHVxQ\">4. FORMAT()<\/h2>\n<p>FORMAT() \u51fd\u6578\u7528\u65bc\u683c\u5f0f\u5316\u65e5\u671f\u548c\u6642\u9593\u3002\u9019\u500b\u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>FORMAT(value, format_string)<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5c07\u7576\u524d\u65e5\u671f\u683c\u5f0f\u5316\u70ba &#8216;yyyy-MM-dd&#8217; \u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT FORMAT(GETDATE(), 'yyyy-MM-dd') AS FormattedDate;<\/code><\/pre>\n<h2 id=\"5-convert-pikAleHVxQ\">5. CONVERT()<\/h2>\n<p>CONVERT() \u51fd\u6578\u7528\u65bc\u5c07\u4e00\u500b\u6578\u64da\u985e\u578b\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u6578\u64da\u985e\u578b\uff0c\u7279\u5225\u662f\u5728\u65e5\u671f\u548c\u6642\u9593\u7684\u8f49\u63db\u4e2d\u975e\u5e38\u6709\u7528\u3002\u5176\u8a9e\u6cd5\u70ba\uff1a<\/p>\n<pre><code>CONVERT(data_type, expression, style)<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5c07\u7576\u524d\u65e5\u671f\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>SELECT CONVERT(VARCHAR, GETDATE(), 101) AS USDateFormat;<\/code><\/pre>\n<h2 id=\"6-year-month-day-pikAleHVxQ\">6. YEAR(), MONTH(), DAY()<\/h2>\n<p>\u9019\u4e9b\u51fd\u6578\u5206\u5225\u7528\u65bc\u63d0\u53d6\u65e5\u671f\u7684\u5e74\u4efd\u3001\u6708\u4efd\u548c\u65e5\u671f\u3002\u5b83\u5011\u7684\u7528\u6cd5\u975e\u5e38\u7c21\u55ae\uff1a<\/p>\n<pre><code>SELECT YEAR(GETDATE()) AS CurrentYear,\n       MONTH(GETDATE()) AS CurrentMonth,\n       DAY(GETDATE()) AS CurrentDay;<\/code><\/pre>\n<h2 id=\"7-%e7%b5%90%e8%ab%96-pikAleHVxQ\">7. \u7d50\u8ad6<\/h2>\n<p>MS SQL Server \u63d0\u4f9b\u4e86\u591a\u7a2e\u65e5\u671f\u6642\u9593\u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u5728\u6578\u64da\u5eab\u4e2d\u6709\u6548\u5730\u8655\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u6642\u9593\u6578\u64da\u3002\u7121\u8ad6\u662f\u7372\u53d6\u7576\u524d\u6642\u9593\u3001\u8a08\u7b97\u65e5\u671f\u5dee\u7570\uff0c\u9084\u662f\u683c\u5f0f\u5316\u65e5\u671f\uff0c\u9019\u4e9b\u51fd\u6578\u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n<p>\u5c0d\u65bc\u9700\u8981\u5728\u9999\u6e2f\u9032\u884c\u6578\u64da\u7ba1\u7406\u7684\u4f01\u696d\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u65b9\u6848\u53ef\u4ee5\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u904b\u884c\u74b0\u5883\uff0c\u9032\u4e00\u6b65\u63d0\u5347\u696d\u52d9\u6548\u7387\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MS SQL Server\u4e2d\u7684\u65e5\u671f\u6642\u9593\u51fd\u6578\uff0c\u638c\u63e1\u5982\u4f55\u6709\u6548\u8655\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u8207\u6642\u9593\u6578\u64da\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-64282","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/64282","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=64282"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/64282\/revisions"}],"predecessor-version":[{"id":64283,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/64282\/revisions\/64283"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=64282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=64282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=64282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}