SUBSTR (mysql)
Date: April 26th 2016
Last updated: April 26th 2016
The substr clause lets you do things like sort a column by a subset of characters.
SUBSTR(column_name, position, length)
Useful resources
- http://stackoverflow.com/questions/8187828/sql-order-by-using-a-substring-within-a-specific-column-possible
- http://www.w3resource.com/mysql/string-functions/mysql-substr-function.php
- http://www.w3resource.com/mysql/string-functions/mysql-char_length-function.php
Example
Select all countries that have a population over 200,000.
Sort the output by the last 4 letters.
select distinct(name)
from my_world_table
where population > 200000
order by SUBSTR(name, CHAR_LENGTH(name)-3);