{"id":1741,"date":"2025-02-16T09:11:34","date_gmt":"2025-02-16T01:11:34","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1741"},"modified":"2025-02-16T09:11:34","modified_gmt":"2025-02-16T01:11:34","slug":"sql-len-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-len-%e5%87%bd%e6%95%b0\/","title":{"rendered":"SQL\u00a0LEN()\u00a0\u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>LEN()<\/code> \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff08\u5373\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6570\u91cf\uff09\u3002\u8be5\u51fd\u6570\u5e38\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4ee5\u4fbf\u5728\u67e5\u8be2\u4e2d\u8fdb\u884c\u8fc7\u6ee4\u3001\u6761\u4ef6\u5224\u65ad\u6216\u6570\u636e\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>LEN()<\/code> \u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>LEN(string);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>string<\/code>\uff1a\u8981\u8ba1\u7b97\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>LEN()<\/code> \u7684\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>employees<\/code> \u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice Johnson<\/td><\/tr><tr><td>2<\/td><td>Bob Smith<\/td><\/tr><tr><td>3<\/td><td>Charlie Brown<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>LEN()<\/code> \u51fd\u6570\u6765\u83b7\u53d6 <code>emp_name<\/code> \u5217\u4e2d\u6bcf\u4e2a\u540d\u5b57\u7684\u957f\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_id, emp_name, LEN(emp_name) AS name_length\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><th>name_length<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice Johnson<\/td><td>14<\/td><\/tr><tr><td>2<\/td><td>Bob Smith<\/td><td>10<\/td><\/tr><tr><td>3<\/td><td>Charlie Brown<\/td><td>14<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>LEN()<\/code> \u51fd\u6570\u8fd4\u56de\u4e86 <code>emp_name<\/code> \u5217\u4e2d\u6bcf\u4e2a\u540d\u5b57\u7684\u5b57\u7b26\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u7528\u4e8e\u8fc7\u6ee4\u5b57\u7b26\u4e32\u957f\u5ea6<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>LEN()<\/code> \u6765\u8fc7\u6ee4\u5b57\u7b26\u4e32\u957f\u5ea6\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u540d\u5b57\u957f\u5ea6\u5927\u4e8e 10 \u7684\u5458\u5de5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_id, emp_name\nFROM employees\nWHERE LEN(emp_name) &gt; 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice Johnson<\/td><\/tr><tr><td>3<\/td><td>Charlie Brown<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>\u7ed3\u5408 <code>LEN()<\/code> \u548c <code>WHERE<\/code> \u8fdb\u884c\u6761\u4ef6\u5224\u65ad<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd8\u53ef\u4ee5\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528 <code>LEN()<\/code> \u6765\u505a\u4e00\u4e9b\u5176\u4ed6\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u6bd4\u5982\u5224\u65ad\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f\u5426\u4e3a\u5076\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_id, emp_name\nFROM employees\nWHERE LEN(emp_name) % 2 = 0;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>emp_id<\/th><th>emp_name<\/th><\/tr><\/thead><tbody><tr><td>2<\/td><td>Bob Smith<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>LEN()<\/code> \u51fd\u6570\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u7a7a\u683c\u95ee\u9898<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LEN()<\/code> \u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u65f6\uff0c\u4e0d\u4f1a\u5ffd\u7565\u672b\u5c3e\u7684\u7a7a\u683c\u3002\u5728\u4e00\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\uff08\u5982 SQL Server\uff09\u4e2d\uff0c<code>LEN()<\/code> \u4f1a\u8ba1\u7b97\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u7a7a\u683c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LEN('Hello    ');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>LEN(&#8216;Hello &#8216;)<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u5ffd\u7565\u672b\u5c3e\u7684\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>RTRIM()<\/code> \u51fd\u6570\u6765\u53bb\u9664\u53f3\u4fa7\u7684\u7a7a\u683c\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u957f\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LEN(RTRIM('Hello    '));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>LEN(&#8216;Hello&#8217;)<\/th><\/tr><\/thead><tbody><tr><td>5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong><code>NULL<\/code> \u503c\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5904\u7406 <code>NULL<\/code> \u503c\u65f6\uff0c<code>LEN()<\/code> \u8fd4\u56de <code>NULL<\/code>\uff0c\u800c\u4e0d\u662f <code>0<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LEN(NULL);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>LEN(NULL)<\/th><\/tr><\/thead><tbody><tr><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b <code>NULL<\/code> \u503c\u8fd4\u56de <code>0<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COALESCE()<\/code> \u6216 <code>ISNULL()<\/code> \u51fd\u6570\u6765\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COALESCE(LEN(emp_name), 0) AS name_length\nFROM employees;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.3 <strong>\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u5dee\u5f02<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u53ef\u80fd\u5728 <code>LEN()<\/code> \u51fd\u6570\u7684\u5b9e\u73b0\u4e0a\u6709\u6240\u4e0d\u540c\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 SQL Server \u548c MySQL \u4e2d\uff0c<code>LEN()<\/code> \u51fd\u6570\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u5728 Oracle \u4e2d\uff0c<code>LEN()<\/code> \u51fd\u6570\u4e0d\u5b58\u5728\uff0c\u5e94\u8be5\u4f7f\u7528 <code>LENGTH()<\/code> \u51fd\u6570\u6765\u4ee3\u66ff\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0cOracle \u67e5\u8be2\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LENGTH(emp_name) AS name_length\nFROM employees;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.4 <strong><code>LEN()<\/code> \u4e0e\u5176\u4ed6\u5b57\u7b26\u4e32\u51fd\u6570\u7684\u7ed3\u5408\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u7ed3\u5408 <code>LEN()<\/code> \u4e0e\u5176\u4ed6\u5b57\u7b26\u4e32\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u3002\u4f8b\u5982\uff0c\u83b7\u53d6 <code>emp_name<\/code> \u5b57\u6bb5\u957f\u5ea6\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u8bb0\u5f55\uff0c\u5e76\u4e14\u5728\u67e5\u8be2\u65f6\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u622a\u53d6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT emp_id, LEFT(emp_name, 5) AS short_name\nFROM employees\nWHERE LEN(emp_name) &gt; 10;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u8fd4\u56de\u957f\u5ea6\u5927\u4e8e 10 \u7684 <code>emp_name<\/code> \u524d 5 \u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>LEN()<\/code> \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/li>\n\n\n\n<li>\u5b83\u4f1a\u8ba1\u7b97\u6240\u6709\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\uff0c\u4f46\u4e0d\u5305\u62ec <code>NULL<\/code> \u503c\u3002<\/li>\n\n\n\n<li>\u5728\u5904\u7406\u7a7a\u683c\u548c <code>NULL<\/code> \u503c\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/li>\n\n\n\n<li>\u5728\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u51fd\u6570\uff08\u5982 <code>LENGTH()<\/code> \u5728 Oracle \u4e2d\uff09\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>LEN() \u662f SQL \u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff08\u5373\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6570\u91cf\uff09\u3002\u8be5\u51fd\u6570\u5e38\u7528\u4e8e\u786e\u5b9a\u5b57\u7b26\u4e32\u7684\u957f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1741","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1741","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=1741"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1741\/revisions"}],"predecessor-version":[{"id":1742,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1741\/revisions\/1742"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}