SELECT * FROM test
WHERE
test.id RLIKE '^[0-9]{2}.[0-9]*$'
To find matches between the values of a column and a specified regular expression use the operator RLIKE
.
In the case the pattern specified in the question is:
- All elements are numbers.
- The third element can be a number or letter.
- In the question it was not specified the length of this value the left variable, and the author of the question should make the adjustment or ask for guidance in the comments.
In the documentation Mysql::Matching patterns is specified:
.
corresponds to any unique character.
A class of character [...]
corresponds to any character between
square brackets. [a-z]
corresponds to any letter, while [0-9]
corresponds to any digit.
*
is zero or more instances than the above.
use ^
and $
to match the beginning and the end.
use the operator {n}
to repeat the pattern n times.
So reading the pattern '^[0-9]{2}.[0-9]*$'
textually the pattern would mean:
Starting with two numeric digits, the third character is letter
or numerical digit ending with zero or more numerical digits.
Example:
CREATE TABLE test (
id varchar(20)
);
INSERT INTO test (id) VALUES ("123456789");
INSERT INTO test (id) VALUES ("12bb00233");
INSERT INTO test (id) VALUES ("12X456789");
INSERT INTO test (id) VALUES ("120c98755");
SELECT * FROM test
WHERE
test.id RLIKE '^[0-9]{2}.[0-9]*$'
Upshot:
id
---------
123456789
12X456789
Testable example in DB Fiddle:
It worked really well at regex. Thank you!
– Pedro Augusto