How to check for uppercase letters in MySQL?

前端 未结 4 1979
再見小時候
再見小時候 2021-01-01 17:15

I want to check, if a string consits only of uppercase letters. I know that RLIKE/REGEXP are not case sensitive in MySQL. So I tried to use the :upper: characte

相关标签:
4条回答
  • 2021-01-01 17:52

    This worked for me to get the list of rows having only upper case characters:

    SELECT 
        name, UPPER(name) 
    FROM table 
    WHERE 
        BINARY name = BINARY UPPER(name)
    ;
    
    0 讨论(0)
  • 2021-01-01 18:03

    For me this works and is not using a regexp. It basically compares the field with itself uppercased by mysql itself.

    -- will detect all names that are not in uppercase
    SELECT 
        name, UPPER(name) 
    FROM table 
    WHERE 
        BINARY name <> BINARY UPPER(name)
    ;
    
    0 讨论(0)
  • 2021-01-01 18:14

    REGEXP is not case sensitive, except when used with binary strings.

    http://dev.mysql.com/doc/refman/5.7/en/regexp.html

    So with that in mind, just do something like this:

    SELECT * FROM `users` WHERE `email` REGEXP BINARY '[A-Z]';
    

    Using the above example, you'd get a list of emails that contain one or more uppercase letters.

    0 讨论(0)
  • 2021-01-01 18:18

    change to case sensitive collation, eg.

    CHARACTER SET latin1 COLLATE latin1_general_cs
    

    then try this query,

    SELECT 'z' REGEXP '^[A-Z]+$'
    
    • SQLFiddle Demo
    0 讨论(0)
提交回复
热议问题