类别:

:doc:`/sql-reference/functions-string`(匹配/比较)

LEFT

返回其输入中最左侧的子字符串。

LEFT(STR, N) 等同于 SUBSTR(STR, 1, N)

另请参阅:

RIGHTSUBSTR、SUBSTRING

语法

LEFT( <string_expr> , <length_expr> )
Copy

实参

string_expr

计算结果为 VARCHAR 或 BINARY 值。

length_expr

计算结果为整数的表达式。它指定:

  • 如果输入为 VARCHAR 值,返回的 UTF-8 字符数。

  • 如果输入为 BINARY 值,返回的字节数。

指定一个大于或等于零的长度。如果长度为负数,该函数将返回空字符串。

返回

返回值的数据类型与 :samp:`{string_expr}`(VARCHAR 或 BINARY)的数据类型相同。

如果任何输入为 NULL,则返回 NULL。

使用说明

如果 length_expr 的长度大于 expr,则该函数返回 expr

排序规则详细信息

  • 排序规则适用于 VARCHAR 输入。如果第一个参数的输入数据类型为 BINARY,则排序规则不适用。

  • No impact. 尽管在语法上可以接受排序规则,但排序规则对处理没有影响。例如,某些语言的字母包含两个字符或三个字符(如匈牙利语中的“dzs”,捷克语中的“ch”),这些字母作为长度实参时仍会算作两个或三个字符(而不是一个字符)。

  • The collation of the result is the same as the collation of the input. 如果返回值作为嵌套函数调用的一部分传递给另一个函数,这可能很有用。

示例

以下示例使用 LEFT 函数。

基本示例

SELECT LEFT('ABCDEF', 3);
Copy
+-------------------+
| LEFT('ABCDEF', 3) |
|-------------------|
| ABC               |
+-------------------+

返回电子邮件地址、电话和日期字符串的子字符串

以下示例返回表中客户信息的子字符串。

创建表并插入数据:

CREATE OR REPLACE TABLE customer_contact_example (
    cust_id INT,
    cust_email VARCHAR,
    cust_phone VARCHAR,
    activation_date VARCHAR)
  AS SELECT
    column1,
    column2,
    column3,
    column4
  FROM
    VALUES
      (1, 'some_text@example.com', '800-555-0100', '20210320'),
      (2, 'some_other_text@example.org', '800-555-0101', '20240509'),
      (3, 'some_different_text@example.net', '800-555-0102', '20191017');

SELECT * from customer_contact_example;
Copy
+---------+---------------------------------+--------------+-----------------+
| CUST_ID | CUST_EMAIL                      | CUST_PHONE   | ACTIVATION_DATE |
|---------+---------------------------------+--------------+-----------------|
|       1 | some_text@example.com           | 800-555-0100 | 20210320        |
|       2 | some_other_text@example.org     | 800-555-0101 | 20240509        |
|       3 | some_different_text@example.net | 800-555-0102 | 20191017        |
+---------+---------------------------------+--------------+-----------------+

结合使用 POSITION 函数和 LEFT 函数,以便从电子邮件地址中提取用户名。此示例找出每个字符串中的 @ 所在位置,然后减去 1 以返回用户名:

SELECT cust_id,
       cust_email,
       LEFT(cust_email, POSITION('@' IN cust_email) - 1) AS username
  FROM customer_contact_example;
Copy
+---------+---------------------------------+---------------------+
| CUST_ID | CUST_EMAIL                      | USERNAME            |
|---------+---------------------------------+---------------------|
|       1 | some_text@example.com           | some_text           |
|       2 | some_other_text@example.org     | some_other_text     |
|       3 | some_different_text@example.net | some_different_text |
+---------+---------------------------------+---------------------+

小技巧

您可以使用 POSITION 函数查找其他字符的位置,例如空字符 (' ') 或下划线 (_)。

在表的 cust_phone 列中,区号始终是前三个字符。从电话号码中提取区号:

SELECT cust_id,
       cust_phone,
       LEFT(cust_phone, 3) AS area_code
  FROM customer_contact_example;
Copy
+---------+--------------+-----------+
| CUST_ID | CUST_PHONE   | AREA_CODE |
|---------+--------------+-----------|
|       1 | 800-555-0100 | 800       |
|       2 | 800-555-0101 | 800       |
|       3 | 800-555-0102 | 800       |
+---------+--------------+-----------+

在表的 activation_date 列中,日期格式始终为 YYYYMMDD。从以下字符串中提取年份:

SELECT cust_id,
       activation_date,
       LEFT(activation_date, 4) AS year
  FROM customer_contact_example;
Copy
+---------+-----------------+------+
| CUST_ID | ACTIVATION_DATE | YEAR |
|---------+-----------------+------|
|       1 | 20210320        | 2021 |
|       2 | 20240509        | 2024 |
|       3 | 20191017        | 2019 |
+---------+-----------------+------+
语言: 中文