Mega Code Archive

 
Categories / MySQL / String
 

SUBSTRING( ) function takes a string and a starting position, returning everything to the right of the positio

n. 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> SELECT name, SUBSTRING(name,4), MID(name,4) FROM mytable; +----------+-------------------+-------------+ | name     | SUBSTRING(name,4) | MID(name,4) | +----------+-------------------+-------------+ | copper   | per               | per         | | gold     | d                 | d           | | iron     | n                 | n           | | lead     | d                 | d           | | mercury  | cury              | cury        | | platinum | tinum             | tinum       | | silver   | ver               | ver         | | tin      |                   |             | +----------+-------------------+-------------+ 8 rows in set (0.00 sec) mysql> mysql> drop table mytable; Query OK, 0 rows affected (0.00 sec)