Consider a table datatbl
like this:
+----------+
| strfield |
+----------+
| abcde |
| fgHIJ |
| KLmno |
+----------+
I want to write a query something like this:
select * from datatbl where strfield rlike '[a-z]*';
As in a non-SQL regex, I'd like to return the lowercase row with abcde
, but not the rows with capitals. I cannot seem to find an easy way to do this. Am I missing something stupid?
The RLIKE condition performs a case-insensitive match, except when used with binary strings.
REGEXP is not case sensitive, except when used with binary strings. It seems to be very sure about that REGEXP without any modifiers will select rows in case insensitive mode.
SQL keywords are by default set to case insensitive, which means that the keywords are allowed to be used in lower or upper case. The names of the tables and columns specification are set to case insensitive on the SQL database server; however, it can be enabled and disabled by configuring the settings in SQL.
By default, it depends on the operating system and its case sensitivity. This means MySQL is case-insensitive in Windows and macOS, while it is case-sensitive in most Linux systems. However, you can change the behavior by changing collation.
The MySQL REGEXP/RLIKE sucks for this - you need to cast the data as BINARY
for case sensitive searching:
SELECT *
FROM datatbl
WHERE CAST(strfield AS BINARY) rlike '[a-z]*';
You'll find this raised in the comments for the REGEXP/RLIKE documentation.
Edit: I've misread OP and this is solution for the opposite case where MySQL is in SENSITIVE collation and you need to compare string in INSENSITIVE way.
You can workaround it using LOWER()
function, too.
SELECT *
FROM datatbl
WHERE LOWER(strfield) RLIKE '[a-z]*';
If you are running MySQL 8+, you can also use case-insensitive switch in REGEXP_LIKE()
function.
SELECT *
FROM datatbl
WHERE REGEXP_LIKE(strfield, '[a-z]*', 'i');
For case-sensitive regex you can use REGEXP_LIKE()
with match type c
like this:
SELECT * FROM `table` WHERE REGEXP_LIKE(`column`, 'value', 'c');
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