MySQL – Select rows from a table when a field starts with a number
A common request is how to select records from a table when a specific field starts with a number. This request often is used when searching free-form text fields where the fields were used to provide an answer field to a question.
The simplest way to attack this is by using a regular expression. For example:
SELECT * FROM BadlyDesignedTable WHERE AnswerColumn regexp ‘^[0-9]+’;
SELECT * FROM BadlyDesignedTable WHERE AnswerColumn RLIKE ‘^[0-9]+’;
Regex and RLIKE are compatible keywords that both represent regular expression matching.
The regex is described as follows :
^ – Start anchor, used to ensure the pattern matches start of the string.
[ – Start of character class.
0-9 – Any digit
] – End of character class
- Topgun Days by Dave “Bio” Baranek – The Truth Behind Topgun and Top Gun
- MySQL – A simple script to truncate all MySQL tables in a Database
Pingback: MySQL Delimiters – Or why I hate stored procedures and Error Code : 1064 You have an error in your SQL syntax. | Prodromus