Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all upper case strings in a MySQL table?

I initially thought this is trivial. Then thought 'binary' might do it. I am unsure at this point.

Name
----
John
MARY
Kin
TED

I would like to query just MARY and TED which are in all upper case. How would I query this?

like image 536
ThinkCode Avatar asked Aug 23 '12 19:08

ThinkCode


4 Answers

If your collation is case insensitive then you need to use a BINARY comparison:

SELECT *
FROM yourtable
WHERE Name = BINARY UPPER(Name)

See it working online: sqlfiddle

like image 55
Mark Byers Avatar answered Nov 03 '22 22:11

Mark Byers


You just use the UPPER() function on the Name field and compare the results with the original value of Name:

select Name from Table where Name = UPPER(Name)

This way

UPPER(Name)   ||  Name
---------------------------------------
JOHN          !=  John
MARY          ==  MARY
KIN           !=  Kin
TED           ==  TED

only the rows you need will be returned.

As @mdoyle commented here, you should define the column with the right collation (case sensitive), otherwise as others did answer you need the BINARY operator to compare case insensitive columns.

like image 43
m_vitaly Avatar answered Nov 03 '22 20:11

m_vitaly


Try this:

select name from table where name=upper(name);
like image 2
kurast Avatar answered Nov 03 '22 21:11

kurast


Try this:

SELECT Name
FROM   table
WHERE  Name COLLATE latin1_general_cs LIKE UPPER(Name)
;
like image 1
Tom Avatar answered Nov 03 '22 22:11

Tom