Trim strings : Trim « String « SQL / MySQL






Trim strings

    
mysql>
mysql> # create a table called "padded"
mysql> CREATE TABLE IF NOT EXISTS padded
    -> (
    ->   id     INT     AUTO_INCREMENT PRIMARY KEY,
    ->   str1 CHAR(10), str2 CHAR(10), str3 CHAR(10)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> # insert 2 records into the "padded" table
mysql> INSERT INTO padded (str1, str2, str3)
    ->   VALUES (" MySQL    ", " Data     ", " Bases    ");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO padded (str1, str2, str3)
    ->   VALUES (" are      ", " great    ", " fun !    ");
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> # show all data in the "padded" table
mysql> SELECT * FROM padded;
+----+--------+--------+--------+
| id | str1   | str2   | str3   |
+----+--------+--------+--------+
|  1 |  MySQL |  Data  |  Bases |
|  2 |  are   |  great |  fun ! |
+----+--------+--------+--------+
2 rows in set (0.00 sec)

mysql>
mysql> # retrieve 2 trimmed concatenated calculated fields
mysql> SELECT CONCAT( TRIM(str1), RTRIM(str2), TRIM(str3) )
    -> FROM padded WHERE id = 1;
+-----------------------------------------------+
| CONCAT( TRIM(str1), RTRIM(str2), TRIM(str3) ) |
+-----------------------------------------------+
| MySQL DataBases                               |
+-----------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT( TRIM(str1), RTRIM(str2), RTRIM(str3) )
    -> FROM padded WHERE id = 2;
+------------------------------------------------+
| CONCAT( TRIM(str1), RTRIM(str2), RTRIM(str3) ) |
+------------------------------------------------+
| are great fun !                                |
+------------------------------------------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> # delete this sample table
mysql> DROP TABLE padded;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>

   
    
    
    
  








Related examples in the same category

1.LTRIM(RTRIM(' SQL '))
2.Trim a string