Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select ALL fields that contains only UPPERCASE letters

Tags:

How do you select a field that contains only uppercase character in mysql or a field that doesn't contain any lower case character?

like image 793
Aivan Monceller Avatar asked Oct 01 '10 05:10

Aivan Monceller


People also ask

How do I select only uppercase in SQL?

The SQL UPPER function converts all the letters in a string into uppercase. If you want to convert a string to lowercase, you use the LOWER function instead.

How do you capitalize everything in SQL?

If you want to display a string in uppercase, use the SQL UPPER() function. This function takes only one argument: the string column that you want to convert to uppercase.

How do you find upper and lower case in SQL?

Case insensitive SQL SELECT: Use upper or lower functionsselect * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

Why is SQL typed in all caps?

All Caps SQL Commands Show example queries with ALL CAPS, First Letter Caps, and lowercase SQL commands. For readability, all SQL commands should be written in uppercase letters. This allows the reader to identify the keywords in the SQL statement and easily determine what the query is executing.


2 Answers

You may want to use a case sensitive collation. I believe the default is case insensitive. Example:

CREATE TABLE my_table (    id int,    name varchar(50) ) CHARACTER SET latin1 COLLATE latin1_general_cs;  INSERT INTO my_table VALUES (1, 'SomeThing'); INSERT INTO my_table VALUES (2, 'something'); INSERT INTO my_table VALUES (3, 'SOMETHING'); INSERT INTO my_table VALUES (4, 'SOME4THING'); 

Then:

SELECT * FROM my_table WHERE name REGEXP '^[A-Z]+$'; +------+-----------+ | id   | name      | +------+-----------+ |    3 | SOMETHING | +------+-----------+ 1 row in set (0.00 sec) 

If you don't want to use a case sensitive collation for the whole table, you can also use the COLLATE clause as @kchau suggested in the other answer.

Let's try with a table using a case insensitive collation:

CREATE TABLE my_table (    id int,    name varchar(50) ) CHARACTER SET latin1 COLLATE latin1_general_ci;  INSERT INTO my_table VALUES (1, 'SomeThing'); INSERT INTO my_table VALUES (2, 'something'); INSERT INTO my_table VALUES (3, 'SOMETHING'); INSERT INTO my_table VALUES (4, 'SOME4THING'); 

This won't work very well:

SELECT * FROM my_table WHERE name REGEXP '^[A-Z]+$'; +------+-----------+ | id   | name      | +------+-----------+ |    1 | SomeThing | |    2 | something | |    3 | SOMETHING | +------+-----------+ 3 rows in set (0.00 sec) 

But we can use the COLLATE clause to collate the name field to a case sensitive collation:

SELECT * FROM my_table WHERE (name COLLATE latin1_general_cs) REGEXP '^[A-Z]+$'; +------+-----------+ | id   | name      | +------+-----------+ |    3 | SOMETHING | +------+-----------+ 1 row in set (0.00 sec) 
like image 197
Daniel Vassallo Avatar answered Sep 24 '22 15:09

Daniel Vassallo


This worked for me. It found all user emails with uppercase character:

SELECT * FROM users WHERE mail REGEXP BINARY '[A-Z]'; 
like image 44
Jasom Dotnet Avatar answered Sep 23 '22 15:09

Jasom Dotnet