Mega Code Archive

 
Categories / MySQL / String
 

Strip off the last three characters (the LENGTH( ) function returns the length of a string)

mysql> mysql> CREATE TABLE mytable     -> (     ->  name    VARCHAR(20)     -> ); Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO mytable (name)     ->  VALUES     ->          ('copper'),     ->          ('gold'),     ->          ('iron'),     ->          ('lead'),     ->          ('mercury'),     ->          ('platinum'),     ->          ('silver'),     ->          ('tin')     -> ; Query OK, 8 rows affected (0.00 sec) Records: 8  Duplicates: 0  Warnings: 0 mysql> mysql> SELECT * FROM mytable; +----------+ | name     | +----------+ | copper   | | gold     | | iron     | | lead     | | mercury  | | platinum | | silver   | | tin      | +----------+ 8 rows in set (0.00 sec) mysql> mysql> mysql> UPDATE mytable SET name = LEFT(name,LENGTH(name)-3); Query OK, 8 rows affected (0.00 sec) Rows matched: 8  Changed: 8  Warnings: 0 mysql> mysql> SELECT name FROM mytable; +-------+ | name  | +-------+ | cop   | | g     | | i     | | l     | | merc  | | plati | | sil   | |       | +-------+ 8 rows in set (0.00 sec) mysql> mysql> drop table mytable; Query OK, 0 rows affected (0.00 sec)