Find Values That Don’t Contain Numbers in MySQL

If you need to return all rows that don’t contain any numbers, the following might help.

Strictly speaking, numbers can be represented by words and other symbols, but for this article “number” simply means “numerical digit”. So we’re finding values that don’t contain any numerical digits.

Example

Suppose we have a table called Products with the following data in its ProductName column:

SELECT ProductName 
FROM Products;

Result:

+-------------------------------------+
| ProductName                         |
+-------------------------------------+
| Left handed screwdriver             |
| Right handed screwdriver            |
| Long Weight (blue)                  |
| Long Weight (green)                 |
| Smash 2000 Sledge Hammer            |
| Chainsaw (Includes 5 spare fingers) |
| Straw Dog Box                       |
| Bottomless Coffee Mugs (4 Pack)     |
+-------------------------------------+

This column contains character data, but some rows contain numbers within that character data (even though they’re not stored as a numeric type).

We can use the following query to return just those rows that don’t contain any numerical digits:

SELECT ProductName 
FROM Products
WHERE ProductName NOT REGEXP '[0-9]+';

Result:

+--------------------------+
| ProductName              |
+--------------------------+
| Left handed screwdriver  |
| Right handed screwdriver |
| Long Weight (blue)       |
| Long Weight (green)      |
| Straw Dog Box            |
+--------------------------+

As expected, only those rows that don’t contain numbers are returned.

Here we used MySQL’s NOT REGEX function to find all rows that matched a pattern. The pattern encompasses all numerical digits from 0 to 9, plus any other character.

This could also be written like this:

SELECT ProductName 
FROM Products
WHERE NOT (ProductName REGEXP '[0-9]+');

Result:

+--------------------------+
| ProductName              |
+--------------------------+
| Left handed screwdriver  |
| Right handed screwdriver |
| Long Weight (blue)       |
| Long Weight (green)      |
| Straw Dog Box            |
+--------------------------+