How to Get Values That Don’t Contain Numbers in MariaDB

When working with MariaDB, you may occasionally find yourself needing to return all rows that don’t contain any numbers.

Technically, 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            |
| Bottomless Coffee Cup               |
| Smash 2000 Sledgehammer             |
| Chainsaw (includes 3 spare fingers) |
| Bottomless Coffee Mugs (4 Pack)     |
+-------------------------------------+

This column obviously 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 |
| Bottomless Coffee Cup    |
+--------------------------+

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

In this case, we used the 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 |
| Bottomless Coffee Cup    |
+--------------------------+

Same output.