How to check for uppercase letters in MySQL?

倖福魔咒の 提交于 2019-12-18 15:35:21

问题


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: character class:

SELECT 'z' REGEXP '^[[:upper:]]+$';

This gives true, although the z is in lower case,... why?


回答1:


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.




回答2:


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)
;



回答3:


change to case sensitive collation, eg.

CHARACTER SET latin1 COLLATE latin1_general_cs

then try this query,

SELECT 'z' REGEXP '^[A-Z]+$'
  • SQLFiddle Demo



回答4:


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)
;


来源:https://stackoverflow.com/questions/16558967/how-to-check-for-uppercase-letters-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!