MySQL Tutorial/String Functions/TRIM

Материал из SQL эксперт
Версия от 09:49, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

If you wanted to trim leading and trailing characters, you would use the keyword BOTH

mysql>
mysql>
mysql> SELECT TRIM(BOTH "XXX" FROM "XXXFileNameXXX");
+----------------------------------------+
| TRIM(BOTH "XXX" FROM "XXXFileNameXXX") |
+----------------------------------------+
| FileName                               |
+----------------------------------------+
1 row in set (0.00 sec)
mysql>


SELECT TRIM(LEADING "XXX" FROM "XXXFileName")

mysql>
mysql> SELECT TRIM(LEADING "XXX" FROM "XXXFileName");
+----------------------------------------+
| TRIM(LEADING "XXX" FROM "XXXFileName") |
+----------------------------------------+
| FileName                               |
+----------------------------------------+
1 row in set (0.00 sec)


The TRIM() function can trim spaces and characters or groups of characters.

mysql>
mysql>
mysql> SELECT TRIM(TRAILING "XXX" FROM "FileName.XXX");
+------------------------------------------+
| TRIM(TRAILING "XXX" FROM "FileName.XXX") |
+------------------------------------------+
| FileName.                                |
+------------------------------------------+
1 row in set (0.00 sec)
mysql>


TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)

Returns the string str with all remstr prefixes or suffixes removed.

If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed.

remstr is optional and, if not specified, spaces are removed.



mysql>
mysql>
mysql> SELECT TRIM("  sqle   ");
+---------------------+
| TRIM("  sqle   ") |
+---------------------+
| sqle              |
+---------------------+
1 row in set (0.00 sec)
mysql>


TRIM(BOTH "x" FROM "xxxjava2sxxx");

mysql>
mysql>
mysql>
mysql> SELECT TRIM(BOTH "x" FROM "xxxsqlexxx");
+------------------------------------+
| TRIM(BOTH "x" FROM "xxxsqlexxx") |
+------------------------------------+
| sqle                             |
+------------------------------------+
1 row in set (0.00 sec)
mysql>


TRIM(LEADING "x" FROM "xxxhava2sxxx");

mysql>
mysql> SELECT TRIM(LEADING "x" FROM "xxxhava2sxxx");
+---------------------------------------+
| TRIM(LEADING "x" FROM "xxxhava2sxxx") |
+---------------------------------------+
| hava2sxxx                             |
+---------------------------------------+
1 row in set (0.01 sec)
mysql>


TRIM(TRAILING "xyz" FROM "java2sxxyz");

mysql>
mysql>
mysql> SELECT TRIM(TRAILING "xyz" FROM "sqlexxyz");
+----------------------------------------+
| TRIM(TRAILING "xyz" FROM "sqlexxyz") |
+----------------------------------------+
| sqlex                                |
+----------------------------------------+
1 row in set (0.01 sec)
mysql>