當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


MySQL TRIM方法用法及代碼示例


MySQL 的 TRIM(~) 方法返回輸入字符串,並刪除指定的前導和/或尾隨字符串 remstr

用法

-- Remove leading and trailing space characters from str
SELECT TRIM(str);
-- Remove leading and trailing remstr string from str
SELECT TRIM(remstr FROM str);
-- Remove both or just the leading or trailing remstr string from str
SELECT TRIM(BOTH | LEADING | TRAILING remstr FROM str);

參數

1. str | string

要從中刪除前導和尾隨字符串的字符串。

2. remstr | string | optional

要刪除的前導/尾隨字符串。默認為' '(空格)。

返回值

刪除指定前導/尾隨字符串 remstr 的輸入字符串。

例子

刪除前導和尾隨字符串

要刪除前導空格和尾隨空格:

SELECT TRIM('         hello        ');



+--------------------------------+
| TRIM('         hello        ') |
+--------------------------------+
| hello                          |
+--------------------------------+

要刪除前導和尾隨字符串 '?'

SELECT TRIM('?' FROM '??????hello???????');



+-------------------------------------+
| TRIM('?' FROM '??????hello???????') |
+-------------------------------------+
| hello                               |
+-------------------------------------+

刪除前導字符串

要僅刪除前導 '? ':

SELECT TRIM(LEADING '?' FROM '??????hello???????');



+---------------------------------------------+
| TRIM(LEADING '?' FROM '??????hello???????') |
+---------------------------------------------+
| hello???????                                |
+---------------------------------------------+

刪除尾隨字符串

要僅刪除尾隨的 '?'

SELECT TRIM(TRAILING '?' FROM '??????hello???????');



+----------------------------------------------+
| TRIM(TRAILING '?' FROM '??????hello???????') |
+----------------------------------------------+
| ??????hello                                  |
+----------------------------------------------+

相關用法


注:本文由純淨天空篩選整理自Arthur Yanagisawa大神的英文原創作品 MySQL | TRIM method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。