{"id":139684,"date":"2024-10-31T19:47:36","date_gmt":"2024-10-31T11:47:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/139684\/"},"modified":"2024-10-31T19:47:36","modified_gmt":"2024-10-31T11:47:36","slug":"sql-server%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%93%8d%e4%bd%9c%e8%a6%81%e7%94%a8%e5%88%b0%e7%9a%84%e5%87%bd%e6%95%b8%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/139684\/","title":{"rendered":"SQL Server\u6578\u64da\u5eab\u4e2d\u5b57\u7b26\u4e32\u64cd\u4f5c\u8981\u7528\u5230\u7684\u51fd\u6578\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<h1 id=\"sql-server%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%93%8d%e4%bd%9c%e8%a6%81%e7%94%a8%e5%88%b0%e7%9a%84%e5%87%bd%e6%95%b8%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f-sICaJxGrAl\">SQL Server\u6578\u64da\u5eab\u4e2d\u5b57\u7b26\u4e32\u64cd\u4f5c\u8981\u7528\u5230\u7684\u51fd\u6578\u6709\u54ea\u4e9b\uff1f<\/h1>\n<p>\u5728SQL Server\u4e2d\uff0c\u5b57\u7b26\u4e32\u64cd\u4f5c\u662f\u6578\u64da\u8655\u7406\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u7121\u8ad6\u662f\u5728\u6578\u64da\u6e05\u7406\u3001\u6578\u64da\u5206\u6790\u9084\u662f\u5831\u544a\u751f\u6210\u4e2d\uff0c\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u9ad8\u6548\u5730\u8655\u7406\u6587\u672c\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6578\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u793a\u4f8b\u4ee5\u4fbf\u65bc\u7406\u89e3\u3002<\/p>\n<h2 id=\"1-len-%e5%87%bd%e6%95%b8-sICaJxGrAl\">1. LEN \u51fd\u6578<\/h2>\n<p>LEN\u51fd\u6578\u7528\u65bc\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u9577\u5ea6\uff08\u5b57\u7b26\u6578\uff09\u3002\u9019\u5c0d\u65bc\u6aa2\u67e5\u6578\u64da\u7684\u5b8c\u6574\u6027\u6216\u78ba\u5b9a\u5b57\u6bb5\u7684\u5927\u5c0f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT LEN('Hello, World!') AS StringLength;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u67e5\u8a62\u5c07\u8fd4\u56de\u5b57\u7b26\u4e32\u201cHello, World!\u201d\u7684\u9577\u5ea6\uff0c\u537313\u3002<\/p>\n<h2 id=\"2-substring-%e5%87%bd%e6%95%b8-sICaJxGrAl\">2. SUBSTRING \u51fd\u6578<\/h2>\n<p>SUBSTRING\u51fd\u6578\u7528\u65bc\u5f9e\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\u5b83\u9700\u8981\u4e09\u500b\u53c3\u6578\uff1a\u539f\u59cb\u5b57\u7b26\u4e32\u3001\u958b\u59cb\u4f4d\u7f6e\u548c\u63d0\u53d6\u7684\u9577\u5ea6\u3002<\/p>\n<pre><code>SELECT SUBSTRING('Hello, World!', 1, 5) AS SubStringResult;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u201cHello\u201d\u3002\u6ce8\u610f\uff0c\u958b\u59cb\u4f4d\u7f6e\u662f\u5f9e1\u958b\u59cb\u8a08\u7b97\u7684\u3002<\/p>\n<h2 id=\"3-charindex-%e5%87%bd%e6%95%b8-sICaJxGrAl\">3. CHARINDEX \u51fd\u6578<\/h2>\n<p>CHARINDEX\u51fd\u6578\u7528\u65bc\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u627e\u5230\uff0c\u5247\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff1b\u5982\u679c\u672a\u627e\u5230\uff0c\u5247\u8fd4\u56de0\u3002<\/p>\n<pre><code>SELECT CHARINDEX('World', 'Hello, World!') AS Position;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de8\uff0c\u56e0\u70ba\u201cWorld\u201d\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u662f\u7b2c8\u500b\u5b57\u7b26\u3002<\/p>\n<h2 id=\"4-replace-%e5%87%bd%e6%95%b8-sICaJxGrAl\">4. REPLACE \u51fd\u6578<\/h2>\n<p>REPLACE\u51fd\u6578\u7528\u65bc\u66ff\u63db\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002\u5b83\u9700\u8981\u4e09\u500b\u53c3\u6578\uff1a\u539f\u59cb\u5b57\u7b26\u4e32\u3001\u8981\u66ff\u63db\u7684\u5b50\u5b57\u7b26\u4e32\u548c\u66ff\u63db\u5f8c\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>SELECT REPLACE('Hello, World!', 'World', 'SQL Server') AS ReplacedString;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u201cHello, SQL Server!\u201d\u3002<\/p>\n<h2 id=\"5-concat-%e5%87%bd%e6%95%b8-sICaJxGrAl\">5. CONCAT \u51fd\u6578<\/h2>\n<p>CONCAT\u51fd\u6578\u7528\u65bc\u5c07\u591a\u500b\u5b57\u7b26\u4e32\u9023\u63a5\u5728\u4e00\u8d77\u3002\u9019\u5728\u751f\u6210\u5831\u544a\u6216\u5408\u4f75\u6578\u64da\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT CONCAT('Hello', ', ', 'World!') AS ConcatenatedString;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u201cHello, World!\u201d\u3002<\/p>\n<h2 id=\"6-upper-%e5%92%8c-lower-%e5%87%bd%e6%95%b8-sICaJxGrAl\">6. UPPER \u548c LOWER \u51fd\u6578<\/h2>\n<p>UPPER\u51fd\u6578\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5927\u5beb\uff0c\u800cLOWER\u51fd\u6578\u5247\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5c0f\u5beb\u3002\u9019\u5c0d\u65bc\u6a19\u6e96\u5316\u6578\u64da\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT UPPER('Hello, World!') AS UpperCaseString, LOWER('Hello, World!') AS LowerCaseString;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u201cHELLO, WORLD!\u201d\u548c\u201chello, world!\u201d\u3002<\/p>\n<h2 id=\"7-trim-%e5%87%bd%e6%95%b8-sICaJxGrAl\">7. TRIM \u51fd\u6578<\/h2>\n<p>TRIM\u51fd\u6578\u7528\u65bc\u53bb\u9664\u5b57\u7b26\u4e32\u958b\u982d\u548c\u7d50\u5c3e\u7684\u7a7a\u683c\u3002\u9019\u5728\u8655\u7406\u7528\u6236\u8f38\u5165\u6642\u7279\u5225\u91cd\u8981\u3002<\/p>\n<pre><code>SELECT TRIM('   Hello, World!   ') AS TrimmedString;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u201cHello, World!\u201d\u3002<\/p>\n<h2 id=\"8-string_agg-%e5%87%bd%e6%95%b8-sICaJxGrAl\">8. STRING_AGG \u51fd\u6578<\/h2>\n<p>STRING_AGG\u51fd\u6578\u5728SQL Server 2017\u53ca\u4ee5\u5f8c\u7248\u672c\u4e2d\u53ef\u7528\uff0c\u7528\u65bc\u5c07\u591a\u884c\u5b57\u7b26\u4e32\u5408\u4f75\u70ba\u4e00\u884c\uff0c\u4e26\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u9694\u3002<\/p>\n<pre><code>SELECT STRING_AGG(Name, ', ') AS AllNames FROM Employees;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6240\u6709\u54e1\u5de5\u59d3\u540d\u7684\u9017\u865f\u5206\u9694\u5217\u8868\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-sICaJxGrAl\">\u7e3d\u7d50<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0c\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6578\u662f\u6578\u64da\u8655\u7406\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u904e\u4f7f\u7528LEN\u3001SUBSTRING\u3001CHARINDEX\u3001REPLACE\u3001CONCAT\u3001UPPER\u3001LOWER\u3001TRIM\u548cSTRING_AGG\u7b49\u51fd\u6578\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u8655\u7406\u548c\u5206\u6790\u6587\u672c\u6578\u64da\u3002\u9019\u4e9b\u51fd\u6578\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u9748\u6d3b\u6027\uff0c\u9084\u80fd\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5229\u7528\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22SQL Server\u6578\u64da\u5eab\u4e2d\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6578\uff0c\u5305\u62ecLEN\u3001SUBSTRING\u3001CHARINDEX\u7b49\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\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-139684","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139684","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=139684"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139684\/revisions"}],"predecessor-version":[{"id":139685,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139684\/revisions\/139685"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=139684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=139684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=139684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}