I'm writing the following statement to select names that starts with a range of characters.
select name from db.table where name like '[c-e]%';
The statement does not return anything to me. But when I change it to:
select name from db.table where name like 'c%';
It returns records.
What is the problem ?
instead of LIKE use REGEXP
select name from db.table where name REGEXP '[c-e].+';
See MySQL Pattern Matching
#juergen d is right but a little change required as you want to check all the name starting from in range [c-d] so
select name from db.table where name REGEXP '^[c-e].+';
"^" indicates that match when starting must match range [c-e]
As others have said, you could use REGEXP; but when you want a range of starting characters, you can also do:
SELECT name FROM db.table WHERE name >= 'c' AND name < 'f';
This could be faster, as it can take advantage of an index in the name field to avoid a full scan.
You should use REGEXP as:
SELECT name FROM db.table where name REGEXP '[c-e].+';
Related
Using MySQL how can I query for all records that start with a certain letter ranging from one letter to another? For example I want to find all entries that have the first letter between a-f.
Matches:
Albert
Donald
Frank
Non Matches:
Sarah
Reba
Thomas
With numbers I can use
SELECT * FROM table WHERE id >= int AND id <= int
Or use the between statement. How can I do that using letters using the first letter of each word in the database?
You should be able to use a range here. To cover a through and including f, regardless of case, we can try:
SELECT *
FROM yourTable
WHERE (name >= 'a' AND name < 'g') OR (name >= 'A' AND name < 'G');
Demo
Note that this approach leaves open the possibility of MySQL being able to use an index which might exist on the name column.
As #John commented below, if you are not using a collation which is case sensitive, then we can simplify the above query to this:
SELECT *
FROM yourTable
WHERE name >= 'a' AND name < 'g';
You can use regular expression for this. Read more here: REGEX_LIKE(). The query you need will be like this:
MySQL 8.0
SELECT
*
FROM
<table_name>
WHERE
REGEXP_LIKE(<column_name>, '^[a-f]');
MySQL < 8.0
SELECT
*
FROM
<table_name>
WHERE
<column_name> REGEXP '^[a-f]';
This will match all register with starting with [a to f] range letters.
I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')
I want to use the LIKE operator to match possible values in a column.
If the value begins with "CU" followed by a digit (e.g. "3") followed by anything else, I would like to return it. There only seems to be a wildcard for any single character using underscore, however I need to make sure it is a digit and not a-z.
I have tried these to no avail:
select name from table1 where name like 'CU[0-9]%'
select name from table1 where name like 'CU#%'
Preferably this could be case sensitive i.e. if cu or Cu or cU then this would not be a match.
You need to use regexp:
select name
from table1
where name regexp binary '^CU[0-9]'
The documentation for regexp is here.
EDIT: binary is required to ensure case-sensitive matching
The like operator only have the % and _ wildcards in MySQL, but you can use a regular expression with the rlike operator:
select name from table1 where name rlike '^CU[0-9]'
You can use REGEXP operator, see http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
so your query would be:
select name from table where name regexp 'CU[0-9].*';
Have you tried with:
select name from table where name between 'CU0' and 'CU9'
I need to run the following MySQL query:
SELECT id, name FROM mytable
WHERE NOT CONTAINS_ANY(name, ';.<>#$!');
except that there doesn't seem to be anything like CONTAINS_ANY (taking two string and checking whether the first string contains any character of those in the second string). What can I do instead? I would like to avoid
SELECT id, name FROM mytable
WHERE name NOT LIKE '%;%'
AND name NOT LIKE '%.%'
AND etc. etc.
and similar ugliness.
Use regex:
SELECT id, name
FROM mytable
WHERE name not rlike '[;.<>#$!]';
In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).
So when i am doing the where condition using REGEXP '^[abc]', I am not getting proper records. How to change the names to lower case and use SELECT QUERY.
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';
This is my query, works fine if the records are lower case, but my records are intermediate ,my all cus name are not lower case , all the names are like ucword.
So for this above query am not getting proper records display.
I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name
Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!
Hepe it helps!
Have you tried:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';
I don't know since when, but nowadays MySql REGEXP is case insensitive.
https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
You don't need regexp to search for names starting with a specific string or character.
SELECT * FROM `test_tbl` WHERE cus_name LIKE 'abc%' ;
% is wildcard char. The search is case insensitive unless you set the binary attribute for column cus_name or you use the binary operator
SELECT * FROM `test_tbl` WHERE BINARY cus_name LIKE 'abc%' ;
A few valid options already presented, but here's one more with just regex:
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abcABC]';