Wildcard for single digit mysql

I want to use the LIKE operator to match possible values ​​in a column.

If the value starts with β€œCU”, followed by a digit (for example, β€œ3”), followed by something else, I would like to return it. It seems like a wildcard is used for any single character, but I need to make sure it's a digit, not az. I tried this to no avail:

select name from table1 where name like 'CU[0-9]%' select name from table1 where name like 'CU#%' 

Preferably, it may be case sensitive, that is, if cu or Cu or cU, then it will not match.

+6
source share
4 answers

You need to use regexp:

 select name from table1 where name regexp binary '^CU[0-9]' 

The documentation for regexp here .

EDIT: binary is required to be case sensitive

+9
source

like operator contains only % and _ characters in MySQL, but you can use regular expression with rlike operator :

 select name from table1 where name rlike '^CU[0-9]' 
+3
source

Have you tried with:

 select name from table where name between 'CU0' and 'CU9' 
+1
source

You can use the REGEXP operator, see http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

so your request will look like this:

 select name from table where name regexp 'CU[0-9].*'; 
+1
source

Source: https://habr.com/ru/post/946532/


All Articles