{"id":64821,"date":"2024-10-14T11:57:16","date_gmt":"2024-10-14T03:57:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/64821\/"},"modified":"2024-10-14T11:57:16","modified_gmt":"2024-10-14T03:57:16","slug":"mssql%e5%bf%ab%e9%80%9f%e7%8d%b2%e5%8f%96%e7%95%b6%e5%89%8d%e6%97%a5%e6%9c%9f%e7%9a%84%e6%96%b9%e6%b3%95%ef%bc%88mssql%e7%8d%b2%e5%be%97%e7%95%b6%e5%89%8d%e6%97%a5%e6%9c%9f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/64821\/","title":{"rendered":"MSSQL\u5feb\u901f\u7372\u53d6\u7576\u524d\u65e5\u671f\u7684\u65b9\u6cd5\uff08mssql\u7372\u5f97\u7576\u524d\u65e5\u671f\uff09"},"content":{"rendered":"<h1 id=\"mssql%e5%bf%ab%e9%80%9f%e7%8d%b2%e5%8f%96%e7%95%b6%e5%89%8d%e6%97%a5%e6%9c%9f%e7%9a%84%e6%96%b9%e6%b3%95%ef%bc%88mssql%e7%8d%b2%e5%be%97%e7%95%b6%e5%89%8d%e6%97%a5%e6%9c%9f%ef%bc%89-bKecKfoJBs\">MSSQL\u5feb\u901f\u7372\u53d6\u7576\u524d\u65e5\u671f\u7684\u65b9\u6cd5\uff08mssql\u7372\u5f97\u7576\u524d\u65e5\u671f\uff09<\/h1>\n<p>\u5728\u4f7f\u7528 Microsoft SQL Server (MSSQL) \u9032\u884c\u6578\u64da\u5eab\u7ba1\u7406\u6642\uff0c\u7372\u53d6\u7576\u524d\u65e5\u671f\u548c\u6642\u9593\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5728\u6578\u64da\u63d2\u5165\u3001\u67e5\u8a62\u9084\u662f\u5831\u544a\u751f\u6210\u4e2d\uff0c\u6e96\u78ba\u7684\u65e5\u671f\u6642\u9593\u4fe1\u606f\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5e7e\u7a2e\u5728 MSSQL \u4e2d\u5feb\u901f\u7372\u53d6\u7576\u524d\u65e5\u671f\u7684\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8-getdate-%e5%87%bd%e6%95%b8-bKecKfoJBs\">1. \u4f7f\u7528 GETDATE() \u51fd\u6578<\/h2>\n<p>GETDATE() \u662f MSSQL \u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6578\u4e4b\u4e00\uff0c\u7528\u65bc\u7372\u53d6\u7576\u524d\u7684\u65e5\u671f\u548c\u6642\u9593\u3002\u9019\u500b\u51fd\u6578\u8fd4\u56de\u4e00\u500b datetime \u985e\u578b\u7684\u503c\uff0c\u8868\u793a\u7576\u524d\u7684\u7cfb\u7d71\u65e5\u671f\u548c\u6642\u9593\u3002<\/p>\n<pre><code>SELECT GETDATE() AS CurrentDateTime;<\/code><\/pre>\n<p>\u57f7\u884c\u4e0a\u8ff0\u67e5\u8a62\u5f8c\uff0c\u60a8\u5c07\u7372\u5f97\u985e\u4f3c\u65bc\u4ee5\u4e0b\u7684\u7d50\u679c\uff1a<\/p>\n<pre><code>CurrentDateTime\n-----------------------\n2023-10-01 14:30:00.000\n<\/code><\/pre>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8-current_timestamp-bKecKfoJBs\">2. \u4f7f\u7528 CURRENT_TIMESTAMP<\/h2>\n<p>\u53e6\u4e00\u500b\u7372\u53d6\u7576\u524d\u65e5\u671f\u548c\u6642\u9593\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 CURRENT_TIMESTAMP\u3002\u9019\u662f\u4e00\u500b ANSI SQL \u6a19\u6e96\u7684\u51fd\u6578\uff0c\u8207 GETDATE() \u7684\u529f\u80fd\u76f8\u540c\u3002<\/p>\n<pre><code>SELECT CURRENT_TIMESTAMP AS CurrentDateTime;<\/code><\/pre>\n<p>\u7d50\u679c\u5c07\u8207\u4f7f\u7528 GETDATE() \u51fd\u6578\u6642\u76f8\u540c\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8-sysdatetime-%e5%87%bd%e6%95%b8-bKecKfoJBs\">3. \u4f7f\u7528 SYSDATETIME() \u51fd\u6578<\/h2>\n<p>\u5982\u679c\u60a8\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\u7684\u65e5\u671f\u548c\u6642\u9593\uff0c\u53ef\u4ee5\u4f7f\u7528 SYSDATETIME() \u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u8fd4\u56de\u4e00\u500b datetime2 \u985e\u578b\u7684\u503c\uff0c\u7cbe\u5ea6\u53ef\u9054\u5230 100 \u7eb3\u79d2\u3002<\/p>\n<pre><code>SELECT SYSDATETIME() AS CurrentDateTime;<\/code><\/pre>\n<p>\u9019\u5c0d\u65bc\u9700\u8981\u9ad8\u7cbe\u5ea6\u6642\u9593\u6233\u7684\u61c9\u7528\u5834\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"4-%e7%8d%b2%e5%8f%96%e7%95%b6%e5%89%8d%e6%97%a5%e6%9c%9f-bKecKfoJBs\">4. \u7372\u53d6\u7576\u524d\u65e5\u671f<\/h2>\n<p>\u5982\u679c\u60a8\u53ea\u9700\u8981\u7576\u524d\u7684\u65e5\u671f\u800c\u4e0d\u9700\u8981\u6642\u9593\uff0c\u53ef\u4ee5\u4f7f\u7528 CAST \u6216 CONVERT \u51fd\u6578\u5c07 GETDATE() \u7684\u7d50\u679c\u8f49\u63db\u70ba\u65e5\u671f\u985e\u578b\u3002<\/p>\n<pre><code>SELECT CAST(GETDATE() AS DATE) AS CurrentDate;<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528 CONVERT \u51fd\u6578\uff1a<\/p>\n<pre><code>SELECT CONVERT(DATE, GETDATE()) AS CurrentDate;<\/code><\/pre>\n<p>\u9019\u5169\u7a2e\u65b9\u6cd5\u90fd\u5c07\u8fd4\u56de\u7576\u524d\u7684\u65e5\u671f\uff0c\u683c\u5f0f\u70ba YYYY-MM-DD\u3002<\/p>\n<h2 id=\"5-%e7%8d%b2%e5%8f%96%e7%95%b6%e5%89%8d%e6%99%82%e9%96%93-bKecKfoJBs\">5. \u7372\u53d6\u7576\u524d\u6642\u9593<\/h2>\n<p>\u5982\u679c\u60a8\u53ea\u9700\u8981\u7576\u524d\u7684\u6642\u9593\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT CAST(GETDATE() AS TIME) AS CurrentTime;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u7576\u524d\u7684\u6642\u9593\uff0c\u683c\u5f0f\u70ba HH:MM:SS\u3002<\/p>\n<h2 id=\"6-%e4%bd%bf%e7%94%a8-datepart-%e5%87%bd%e6%95%b8%e7%8d%b2%e5%8f%96%e7%89%b9%e5%ae%9a%e9%83%a8%e5%88%86-bKecKfoJBs\">6. \u4f7f\u7528 DATEPART \u51fd\u6578\u7372\u53d6\u7279\u5b9a\u90e8\u5206<\/h2>\n<p>\u5982\u679c\u60a8\u53ea\u60f3\u7372\u53d6\u7576\u524d\u65e5\u671f\u7684\u7279\u5b9a\u90e8\u5206\uff08\u5982\u5e74\u3001\u6708\u3001\u65e5\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 DATEPART \u51fd\u6578\u3002<\/p>\n<pre><code>SELECT \n    DATEPART(YEAR, GETDATE()) AS CurrentYear,\n    DATEPART(MONTH, GETDATE()) AS CurrentMonth,\n    DATEPART(DAY, GETDATE()) AS CurrentDay;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u7576\u524d\u7684\u5e74\u4efd\u3001\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-bKecKfoJBs\">\u7e3d\u7d50<\/h2>\n<p>\u5728 MSSQL \u4e2d\uff0c\u7372\u53d6\u7576\u524d\u65e5\u671f\u548c\u6642\u9593\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5f9e GETDATE() \u5230 SYSDATETIME()\uff0c\u6bcf\u7a2e\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u3002\u6839\u64da\u60a8\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u51fd\u6578\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 MSSQL \u6578\u64da\u5eab\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7528\u6236\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728MSSQL\u4e2d\u5feb\u901f\u7372\u53d6\u7576\u524d\u65e5\u671f\u7684\u65b9\u6cd5\uff0c\u7c21\u55ae\u6613\u7528\uff0c\u63d0\u5347\u6578\u64da\u67e5\u8a62\u6548\u7387\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-64821","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/64821","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=64821"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/64821\/revisions"}],"predecessor-version":[{"id":64822,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/64821\/revisions\/64822"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=64821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=64821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=64821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}