{"id":975,"date":"2025-01-15T00:44:28","date_gmt":"2025-01-14T16:44:28","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=975"},"modified":"2025-01-15T00:44:28","modified_gmt":"2025-01-14T16:44:28","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-t-sql-%e4%b8%ad%e6%9c%89%e6%95%88%e5%a1%ab%e5%85%85-varchar-%e5%80%bc%ef%bc%9f%e6%9c%80%e5%b8%b8%e8%a7%81%e7%9a%84%e5%a1%ab%e5%85%85%e6%96%b9%e6%b3%95%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/15\/%e5%a6%82%e4%bd%95%e5%9c%a8-t-sql-%e4%b8%ad%e6%9c%89%e6%95%88%e5%a1%ab%e5%85%85-varchar-%e5%80%bc%ef%bc%9f%e6%9c%80%e5%b8%b8%e8%a7%81%e7%9a%84%e5%a1%ab%e5%85%85%e6%96%b9%e6%b3%95%e8%a7%a3%e6%9e%90\/","title":{"rendered":"\u5982\u4f55\u5728 T-SQL \u4e2d\u6709\u6548\u586b\u5145 VARCHAR \u503c\uff1f\u6700\u5e38\u89c1\u7684\u586b\u5145\u65b9\u6cd5\u89e3\u6790"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 T-SQL\uff08SQL Server\uff09\u4e2d\uff0c\u586b\u5145 <code>VARCHAR<\/code> \u503c\u901a\u5e38\u662f\u6307\u8c03\u6574\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4f7f\u5176\u6ee1\u8db3\u7279\u5b9a\u8981\u6c42\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u586b\u5145\u64cd\u4f5c\uff0c\u4f7f\u5176\u586b\u5145\u4e3a\u6307\u5b9a\u957f\u5ea6\uff0c\u5e38\u89c1\u7684\u586b\u5145\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u7a7a\u683c\u3001\u96f6\u3001\u6216\u5176\u4ed6\u5b57\u7b26\u8fdb\u884c\u586b\u5145\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5728 T-SQL \u4e2d\u6709\u6548\u586b\u5145 <code>VARCHAR<\/code> \u503c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>RIGHT<\/code> \u6216 <code>LEFT<\/code> \u51fd\u6570\u586b\u5145\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>RIGHT<\/code> \u548c <code>LEFT<\/code> \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u4ece\u5b57\u7b26\u4e32\u7684\u53f3\u4fa7\u6216\u5de6\u4fa7\u586b\u5145\u7279\u5b9a\u6570\u91cf\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>RIGHT<\/code> \u586b\u5145\uff1a<\/strong> \u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u7684\u53f3\u4fa7\u6dfb\u52a0\u5b57\u7b26\uff0c\u76f4\u5230\u8fbe\u5230\u6307\u5b9a\u957f\u5ea6\u3002<\/li>\n\n\n\n<li><strong><code>LEFT<\/code> \u586b\u5145\uff1a<\/strong> \u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u7684\u5de6\u4fa7\u6dfb\u52a0\u5b57\u7b26\uff0c\u76f4\u5230\u8fbe\u5230\u6307\u5b9a\u957f\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a <code>VARCHAR<\/code> \u5b57\u7b26\u4e32\uff0c\u5b83\u7684\u539f\u59cb\u957f\u5ea6\u4e3a 3\uff0c\u60f3\u8981\u586b\u5145\u5b83\u5230 10 \u4e2a\u5b57\u7b26\u957f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u4f7f\u7528 RIGHT \u586b\u5145\nSELECT RIGHT('000' + '123', 10) AS FilledValue;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u4f1a\u5728\u5b57\u7b26\u4e32 <code>123<\/code> \u7684\u5de6\u4fa7\u6dfb\u52a0\u96f6\uff0c\u76f4\u5230\u5b57\u7b26\u4e32\u603b\u957f\u5ea6\u4e3a 10\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FilledValue\n-------------\n0000000123<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u4f3c\u7684\uff0c\u4f7f\u7528 <code>LEFT<\/code> \u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u4f7f\u7528 LEFT \u586b\u5145\nSELECT LEFT('123' + '000', 10) AS FilledValue;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u4f1a\u5c06 <code>'123'<\/code> \u53f3\u4fa7\u586b\u5145\u96f6\uff0c\u76f4\u5230\u603b\u957f\u5ea6\u4e3a 10\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>REPLICATE<\/code> \u51fd\u6570\u586b\u5145<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>REPLICATE<\/code> \u51fd\u6570\u7528\u4e8e\u91cd\u590d\u6307\u5b9a\u5b57\u7b26\u591a\u6b21\u3002\u901a\u8fc7\u5c06\u8be5\u51fd\u6570\u4e0e\u5b57\u7b26\u4e32\u64cd\u4f5c\u7ed3\u5408\uff0c\u53ef\u4ee5\u586b\u5145 <code>VARCHAR<\/code> \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5c06 <code>VARCHAR<\/code> \u5b57\u7b26\u4e32\u586b\u5145\u5230\u6307\u5b9a\u957f\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u4f7f\u7528 REPLICATE \u586b\u5145\nSELECT REPLICATE('0', 10 - LEN('123')) + '123' AS FilledValue;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u4f1a\u5148\u8ba1\u7b97 <code>123<\/code> \u7684\u957f\u5ea6\uff083\uff09\uff0c\u7136\u540e\u7528 <code>REPLICATE<\/code> \u51fd\u6570\u5728\u5176\u5de6\u4fa7\u6dfb\u52a0\u96f6\uff0c\u76f4\u5230\u603b\u957f\u5ea6\u4e3a 10\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FilledValue\n-------------\n0000000123<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>FORMAT<\/code> \u51fd\u6570\u8fdb\u884c\u6570\u5b57\u683c\u5f0f\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u9700\u8981\u5bf9\u6570\u5b57\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u4f7f\u5176\u6210\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>FORMAT<\/code> \u51fd\u6570\uff08\u4ec5\u9002\u7528\u4e8e SQL Server 2012 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\u3002<code>FORMAT<\/code> \u51fd\u6570\u53ef\u4ee5\u683c\u5f0f\u5316\u65e5\u671f\u3001\u65f6\u95f4\u3001\u6570\u5b57\u7b49\u7c7b\u578b\u7684\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u6570\u5b57 <code>123<\/code> \u683c\u5f0f\u5316\u4e3a\u4e00\u4e2a 10 \u4f4d\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u96f6\u586b\u5145\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT FORMAT(123, '0000000000') AS FilledValue;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u4f1a\u5c06\u6570\u5b57 <code>123<\/code> \u586b\u5145\u4e3a 10 \u4f4d\uff0c\u5de6\u4fa7\u8865\u96f6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FilledValue\n-------------\n0000000123<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>CONCAT<\/code> \u548c <code>SPACE<\/code> \u51fd\u6570\u586b\u5145\u7a7a\u683c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u9700\u8981\u586b\u5145 <code>VARCHAR<\/code> \u5b57\u7b26\u4e32\u5e76\u4f7f\u7528\u7a7a\u683c\uff08\u5982\u53f3\u5bf9\u9f50\u6587\u672c\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>CONCAT<\/code> \u51fd\u6570\u4e0e <code>SPACE<\/code> \u51fd\u6570\u7ed3\u5408\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u4f7f\u7528 CONCAT \u548c SPACE \u586b\u5145\nSELECT CONCAT('123', SPACE(10 - LEN('123'))) AS FilledValue;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u67e5\u8be2\u4f1a\u5c06\u5b57\u7b26\u4e32 <code>'123'<\/code> \u53f3\u4fa7\u586b\u5145\u7a7a\u683c\uff0c\u76f4\u5230\u603b\u957f\u5ea6\u4e3a 10\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FilledValue\n-------------\n123        <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u586b\u5145\u56fa\u5b9a\u957f\u5ea6\u7684\u5217<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u8bb0\u5f55\u7684\u8868\uff0c\u5e76\u4e14\u5e0c\u671b\u5c06\u6bcf\u4e2a\u5b57\u7b26\u4e32\u5b57\u6bb5\u586b\u5145\u4e3a\u56fa\u5b9a\u957f\u5ea6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\u7684\u4efb\u610f\u4e00\u79cd\u5728\u67e5\u8be2\u4e2d\u586b\u5145\u6bcf\u4e00\u884c\u7684\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2a <code>Products<\/code> \u8868\uff0c\u5305\u542b\u4e00\u4e2a <code>ProductCode<\/code> \u5217\uff0c\u4f60\u5e0c\u671b\u586b\u5145\u8be5\u5217\u4e2d\u7684\u503c\uff0c\u786e\u4fdd\u6bcf\u4e2a <code>ProductCode<\/code> \u90fd\u6709 10 \u4e2a\u5b57\u7b26\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    ProductCode,\n    RIGHT('0000000000' + ProductCode, 10) AS PaddedProductCode\nFROM Products;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u67e5\u8be2\u4f1a\u5c06\u6bcf\u4e2a <code>ProductCode<\/code> \u586b\u5145\u81f3 10 \u4e2a\u5b57\u7b26\u957f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 T-SQL \u4e2d\uff0c\u586b\u5145 <code>VARCHAR<\/code> \u503c\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>RIGHT<\/code> \u548c <code>LEFT<\/code> \u51fd\u6570\u6765\u586b\u5145\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>REPLICATE<\/code> \u51fd\u6570\u5728\u5b57\u7b26\u4e32\u7684\u6307\u5b9a\u4f4d\u7f6e\u586b\u5145\u5b57\u7b26\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>FORMAT<\/code> \u51fd\u6570\u5bf9\u6570\u5b57\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>CONCAT<\/code> \u548c <code>SPACE<\/code> \u51fd\u6570\u8fdb\u884c\u7a7a\u683c\u586b\u5145\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\uff08\u5982\u586b\u5145\u5b57\u7b26\u7684\u79cd\u7c7b\u548c\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff09\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u6709\u6548\u5730\u5e2e\u52a9\u4f60\u5728 SQL \u67e5\u8be2\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u586b\u5145\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 T-SQL\uff08SQL Server\uff09\u4e2d\uff0c\u586b\u5145 VARCHAR \u503c\u901a\u5e38\u662f\u6307\u8c03\u6574\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4f7f\u5176\u6ee1\u8db3\u7279\u5b9a\u8981\u6c42\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-975","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=975"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/975\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/975\/revisions\/976"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}