Select ALL fields that contains only UPPERCASE letters

前端 未结 8 1571
忘了有多久
忘了有多久 2020-12-06 09:16

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

相关标签:
8条回答
  • 2020-12-06 09:52

    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)
    
    0 讨论(0)
  • 2020-12-06 09:52
    SELECT column_name FROM table WHERE column_name REGEXP BINARY '^[A-Z]+$'
    
    0 讨论(0)
  • 2020-12-06 10:01

    Try this -

    SELECT * FROM <mytable> WHERE UPPER(<columnname>) = <columnname>
    
    0 讨论(0)
  • 2020-12-06 10:03

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

    SELECT * FROM users WHERE mail REGEXP BINARY '[A-Z]';
    
    0 讨论(0)
  • 2020-12-06 10:04

    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:]+]$'
    
    0 讨论(0)
  • 2020-12-06 10:10

    Basic eg.

    SELECT * FROM foo WHERE bar REGEXP '[A-Z]';
    
    0 讨论(0)
提交回复
热议问题