SQL TRIM() 函数用于从字符串中删除指定的字符,通常是空格
SELECT TRIM(' Hello, World! ') AS trimmed_string;
输出结果:
+---------------+
| trimmed_string |
+---------------+
| Hello, World! |
+---------------+
SELECT LTRIM(' Hello, World!') AS left_trimmed_string;
输出结果:
+------------------+
| left_trimmed_string |
+------------------+
| Hello, World! |
+------------------+
SELECT RTRIM('Hello, World! ') AS right_trimmed_string;
输出结果:
+-------------------+
| right_trimmed_string |
+-------------------+
| Hello, World! |
+-------------------+
SELECT TRIM(BOTH '*' FROM '***Hello, World!***') AS trimmed_string;
输出结果:
+---------------+
| trimmed_string |
+---------------+
| Hello, World! |
+---------------+
假设有一个名为 employees
的表,其中包含一个名为 first_name
的列,我们想要查询所有以 “A” 开头的员工:
SELECT * FROM employees WHERE TRIM(LEADING 'A' FROM first_name) <> first_name;
这将返回所有以 “A” 开头并且在 “A” 之后有其他字符的员工记录。