在SQL中,TRIM函數用于去除字符串的首尾空格或指定字符。其語法如下:
```sql
TRIM([LEADING | TRAILING | BOTH] [characters FROM] string)
```
參數說明:
- LEADING: 去除字符串開頭的指定字符
- TRAILING: 去除字符串末尾的指定字符
- BOTH: 同時去除字符串開頭和末尾的指定字符
- characters: 要去除的字符,默認為首尾空格
- string: 要去除空格或指定字符的字符串
示例:
```sql
SELECT TRIM(' hello ') AS trimmed_string; -- 結果為 'hello'
SELECT TRIM(LEADING '0' FROM '00012345') AS trimmed_string; -- 結果為 '12345'
SELECT TRIM(BOTH '0' FROM '00012345') AS trimmed_string; -- 結果為 '12345'
SELECT TRIM(TRAILING '0' FROM '12345000') AS trimmed_string; -- 結果為 '12345'
```