How to check for uppercase letters in MySQL? - mysql

I want to check if a string consists only of uppercase letters. I know that RLIKE/REGEXP are not case sensitive in MySQL, so I tried to use the :upper: character class:
SELECT 'z' REGEXP '^[[:upper:]]+$';
This gives true, although the z is in lower case. Why is that?

REGEXP is not case sensitive, except when used with binary strings.
http://dev.mysql.com/doc/refman/5.7/en/regexp.html
So with that in mind, just do something like this:
SELECT * FROM `users` WHERE `email` REGEXP BINARY '[A-Z]';
Using the above example, you'd get a list of emails that contain one or more uppercase letters.

For me this works and is not using a regexp. It basically compares the field with itself uppercased by mysql itself.
-- will detect all names that are not in uppercase
SELECT
name, UPPER(name)
FROM table
WHERE
BINARY name <> BINARY UPPER(name)
;

change to case sensitive collation, eg.
CHARACTER SET latin1 COLLATE latin1_general_cs
then try this query,
SELECT 'z' REGEXP '^[A-Z]+$'
SQLFiddle Demo

The most voted answer doesn't work for me, I get the error:
Character set 'utf8mb4_unicode_ci' cannot be used in conjunction with 'binary' in call to regexp_like.
I used the MD5 to compare the original value and the lowercased value:
SELECT * FROM user WHERE MD5(email) <> MD5(LOWER(email));

Related

Mysql LIKE 2nd character and 2nd last character both are 'm'

I try to find query to find a string that 2nd character and 2nd last character both are letter m.
SELECT last_name
FROM employees
WHERE (last_name LIKE '_m%m_' AND LENGTH(last_name) >= '3');
Thanks in advance :)
Why not just OR instead of AND? I don't see the point of AND when your LIKE operator allready rules out names below three characters. You don't need to use regex nor a check for length:
SELECT last_name FROM employees
WHERE last_name LIKE '_m_'
OR last_name LIKE '_m%m_';
The use of OR and LIKE does catch any string that has at least 3 characters.
If you must use regex, try REGEXP operator:
SELECT last_name FROM employees WHERE last_name REGEXP '^.m(.*m)?.$';
Where the pattern means:
^.m - Start-line anchor with a single character and a literal 'm';
(.*m)? - Optional capture group to match 0+ characters upto a literal 'm';
.$ - A single character with end-line anchor.
The benefit of REGEXP is that it's a bit less verbose if you need case-insensitive matching using pattern: '^.[Mm](.*[Mm])?.$'. See an online demo.
If you need all record with second and last character is m you can use the following query:
select * from <table> where <column> like '_m%m'
the _ in the query is a placeholder for one character and % for many characters

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

sql : select uppercase columns in database

I need to select columns that end in an uppercase extension.
for example, look at the following table.
id - picture
1 - abc.JPG
2 - def.jpg
3 - 123.jpg
4 - xyz.JPG
the results should give me the rows 1 and 4 because JPG are in uppercase.
can anyone help?
I'm far from an expert, but case sensitivity has hung me up before. Are you able to modify the structure of your table? One thing that may help is changing the collation of the table as follows (SQLFiddle here):
CREATE TABLE pics (id INT, picture VARCHAR(200))
CHARACTER SET latin1 COLLATE latin1_general_cs;
INSERT INTO pics VALUES
(1, 'abc.JPG'),
(2, 'def.jpg'),
(3, '123.jpg'),
(4, 'xyz.JPG')
The _cs stands for case sensitive, and I believe the default is case insensitive, which makes case-based comparisons a bit trickier. You can then use the following query to get your rows:
SELECT *
FROM pics
WHERE picture REGEXP '\.[[:upper:]+]$'
If you do not have access to your underlying table, you could try the following, which casts the column in a different character set (latin1), and then changes the collation to support case-insensitive comparisons (SQLFiddle here):
SELECT *
FROM pics
WHERE CAST(picture AS CHAR CHARACTER SET latin1)
COLLATE latin1_general_cs REGEXP '\.[[:upper:]+]$'
Some REGEXPs:
'[.][[:upper:]]+$' -- just uppercase letters
'[.].*[[:upper:]]' -- at least one uppercase letter
'[.].*[[:lower:]]' -- at least one lowercase letter; AND together with previous to get upper and lower, etc.
If there could be two "." in the filename, then consider using
SUBSTRING_INDEX(picture, '.', -1)
to isolate the 'extension'.
Most SQL languages have a UCASE or UPPER function to convert text to uppercase. I'm also taking advantage of the RIGHT function which isn't in all SQL dialects. If your SQL doesn't have a RIGHT function you'll have to futz with SUBSTRING and LENGTH to get the right three characters in picture.
Select id, picture
from table
where UPPER(RIGHT(TRIM(picture),3)) = RIGHT(TRIM(picture),3)
If the same text converted to uppercase is the same as the unconverted text then it is uppercase in the database and will be selected.
As it can not always be assumed that the file extension will be 3 letters you may use the following to get the first chart after the period and compare it to see if it is uppercase:
select * from table where SUBSTRING(picture,CHARINDEX('.',picture) + 1,1)
= upper(SUBSTRING(picture,CHARINDEX('.',picture) + 1,1)) collate SQL_Latin1_General_CP1_CS_AS

MySQL DB selects records with and without umlauts. e.g: '.. where something = FÖÖ'

My Table collation is "utf8_general_ci". If i run a query like:
SELECT * FROM mytable WHERE myfield = "FÖÖ"
i get results where:
... myfield = "FÖÖ"
... myfield = "FOO"
is this the default for "utf8_general_ci"?
What collation should i use to only get records where myfield = "FÖÖ"?
SELECT * FROM table WHERE some_field LIKE ('%ö%' COLLATE utf8_bin)
A list of the collations offered by MySQL for Unicode character sets can be found here:
http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html
If you want to go all-out and require strings to be absolutely identical in order to test as equal, you can use utf8_bin (the binary collation). Otherwise, you may need to do some experimentation with the different collations on offer.
For scandinavian letters you can use utf8_swedish_ci fir example.
Here is the character grouping for utf8_swedish_ci. It shows which characters are interpreted as the same.
http://collation-charts.org/mysql60/mysql604.utf8_swedish_ci.html
Here's the directory listing for other collations. I'm no sure which is the used utf8_general_ci though. http://collation-charts.org/mysql60/

How can I search (case-insensitive) in a column using LIKE wildcard?

I looked around some and didn't find what I was after so here goes.
SELECT * FROM trees WHERE trees.`title` LIKE '%elm%'
This works fine, but not if the tree is named Elm or ELM etc...
How do I make SQL case insensitive for this wild-card search?
I'm using MySQL 5 and Apache.
I've always solved this using lower:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE '%elm%'
SELECT *
FROM trees
WHERE trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'
Actually, if you add COLLATE UTF8_GENERAL_CI to your column's definition, you can just omit all these tricks: it will work automatically.
ALTER TABLE trees
MODIFY COLUMN title VARCHAR(…) CHARACTER
SET UTF8 COLLATE UTF8_GENERAL_CI.
This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'
The case sensitivity is defined in the columns / tables / database collation settings. You can do the query under a specific collation in the following way:
SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci
for instance.
(Replace utf8_general_ci with whatever collation you find useful). The _ci stands for case insensitive.
This is the example of a simple LIKE query:
SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'
Now, case-insensitive using LOWER() func:
SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')
Simply use :
"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE '%elm%'";
Or Use
"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE '%elm%'";
Both functions works same
I'm doing something like that.
Getting the values in lowercase and MySQL does the rest
$string = $_GET['string'];
mysqli_query($con,"SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER('%$string%')");
And For MySQL PDO Alternative:
$string = $_GET['string'];
$q = "SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER(?);";
$query = $dbConnection->prepare($q);
$query->bindValue(1, "%$string%", PDO::PARAM_STR);
$query->execute();
use ILIKE
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
it worked for me !!
Non-binary string comparisons (including LIKE) are case insensitive by default in MySql:
https://dev.mysql.com/doc/refman/en/case-sensitivity.html
I think this query will do a case insensitive search:
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
You don't need to ALTER any table. Just use the following queries, prior to the actual SELECT query that you want to use the wildcard:
set names `utf8`;
SET COLLATION_CONNECTION=utf8_general_ci;
SET CHARACTER_SET_CLIENT=utf8;
SET CHARACTER_SET_RESULTS=utf8;
well in mysql 5.5 , like operator is insensitive...so if your vale is elm or ELM or Elm or eLM or any other , and you use like '%elm%' , it will list all the matching values.
I cant say about earlier versions of mysql.
If you go in Oracle , like work as case-sensitive , so if you type like '%elm%' , it will go only for this and ignore uppercases..
Strange , but this is how it is :)
SELECT name
FROM gallery
WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%'
GROUP BY name COLLATE utf8_general_ci LIMIT 5
You must set up proper encoding and collation for your tables.
Table encoding must reflect the actual data encoding. What is your data encoding?
To see table encoding, you can run a query SHOW CREATE TABLE tablename
When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion
I've always solved like this:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE LOWER('%elm%');
For example if you want to search name like Raja not raja, Royal not royal etc, add BINARY before column name in WHERE clause.
SELECT name FROM person_tbl
WHERE BINARY name LIKE "R%";