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” 之后有其他字符的員工記錄。