How to check if a value does not contain numberical digit in MariaDB

This article provides an example for checking that a value does not contain numberical digit in MariaDB.

Posted on

In a previous tutorial, you learned how to check that a value contains numbers in MariaDB. If you need to check that one does not contain a number, you need to use the NOT REGEXP operator.

NOT REGEXP is the negation of the REGEXP operator.

Example

Suppose we have a notes table with the following rows:

SELECT * FROM notes;

Output:

+----+------------------------+
| id | note                   |
+----+------------------------+
|  1 | Hello World            |
|  2 | This is for you, 2022. |
|  3 | AAAA                   |
|  4 | B-2-9-6                |
|  5 | 1234567890             |
+----+------------------------+

Filter numbers

The following statement finds rows that contain numbers in the note column:

SELECT *
FROM notes
WHERE note REGEXP '[0-9]+';

Output:

+----+------------------------+
| id | note                   |
+----+------------------------+
|  2 | This is for you, 2022\. |
|  4 | B-2-9-6                |
|  5 | 1234567890             |
+----+------------------------+

If you need to find rows that do not contain numbers ​​in the note column, please use the NOT REGEXP operator, as follows:

SELECT *
FROM notes
WHERE note NOT REGEXP '[0-9]+';

Output:

+----+-------------+
| id | note        |
+----+-------------+
|  1 | Hello World |
|  3 | AAAA        |
+----+-------------+

Conclusion

In this article, we use NOT REGEXP the operator to implement the negation of the REGEXP operator.