MySQL regex query case insensitive - mysql

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]';

Related

Can anyone tell me in mysql How to display employee names whose name DO NOT start with alphabet A?

I am a beginner so please help me.
There are 2 things you need to combine in this case.
Because you didn't provide enough information in your question we have to guess what you mean by name. I'm going to assume that you have a single name column, but that would be unusual.
With strings, to match a character column that is not an exact match, you need to use LIKE which allows for wildcards.
You also need to negate the match, or in other words show things that are NOT (something).
First to match names that START with 'A'.
SELECT * FROM table_name WHERE name LIKE 'A%';
This should get you all the PEOPLE who have names that "Start with A".
Some databases are case sensitive. I'm not going to deal with that issue. If you were using MySQL that is not an issue. Case sensitivity is not universal. In some RDBMS like Oracle you have to take some steps to deal with mixed case in a column.
Now to deal with what you actually want, which is NOT (starting with A).
SELECT * FROM table_name WHERE name NOT LIKE 'A%';
your question should have more detail however you can use the substr function
SELECT name FROM yourtable
WHERE SUBSTR(name,1,1) <> 'A'
complete list of mysql string functions here
mysql docs
NOT REGXP operator
MySQL NOT REGXP is used to perform a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression.
Syntax:
expr NOT REGEXP pat
Query:
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^[a]';
or
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^a';

Show/convert only alphanumeric data in sql query [duplicate]

I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine

wildcard for single digit mysql

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'

Mysql statement to select names that start with specific range of characters

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].+';

How to find certain Hex values and Char() Values in a MySQL SELECT

Anyone know the syntax to search a MySQL table column by hex value or CHAR() value? Do I have to use a LIKE or IN()?
Something like:
SELECT * FROM `myWordTable` WHERE `definition` LIKE 0xE28098;
OR
SELECT * FROM `myWordTable` WHERE `definition` LIKE CHAR(128);
Thanks!
The second one is close.
Try this:
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE concat('%',CHAR(128),'%');
In the first example consider pattern match '%' == 0x25
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE 0x25E2809825;
You need to UNHEX() values you want to match on.
For example, finding all accented e's that were corrupted into 0xC3A9:
SELECT id, FirstName
FROM Person
WHERE FirstName
RLIKE UNHEX('c3a9');