tags:

views:

45

answers:

5

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

A: 

By using REGEXP : http://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/

Use [:upper:] for uppercase letters.

SELECT * FROM table WHERE field REGEXP '^[[:upper:]+]$'

Ruel
using [:upper:] would also select 'Example' .. [^a-z] doesn't seem to work fine
geocine
edited. Test it. :)
Ruel
A: 

Basic eg.

SELECT * FROM foo WHERE bar REGEXP '[A-Z]';

kchau
not working thanks SELECT 'as' REGEXP '[A-Z]' returns 1 SELECT 'AS' REGEXP '[A-Z]' returns 1
geocine
Can your field contain other characters, such as spaces, etc? If so, you would want to include those in the regular expression pattern as well.
kchau
A: 

Try this -

select * from <mytable> where upper(<columnname>) = <columnname>
Sachin Shanbhag
@In Sane - Thanks. Yes you are right. Edited my answer.
Sachin Shanbhag
That will not work with a case insensitive collation. In addition, that will also select fields that contain numbers and other characters. I don't think the OP wants that.
Daniel Vassallo
@Daniel - Yes, I agree this does not handle special characters. But the question is not clear about he wants or does not want to retain special characters.
Sachin Shanbhag
A: 
select * from table1 where (columnname COLLATE latin1_bin )=upper(depart);
kchau
That will also select fields that contain numbers and other characters. I don't think the OP wants that.
Daniel Vassallo
+3  A: 

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)
Daniel Vassallo
another way is to use BINARY compare BINARY city_name = BINARY upper(city_name)
geocine