How do you select a field that contains only uppercase character in mysql or a field that doesn't contain any lower case character?
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.
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.
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.
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.
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)
This worked for me. It found all user emails with uppercase character:
SELECT * FROM users WHERE mail REGEXP BINARY '[A-Z]';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With