Most RDBMSs provide at least a few ways to return rows that contain lowercase characters. Here are some options available in the major RDBMSs.
SQL Server
In SQL Server, we can use the UPPER()
function to compare the original value to its uppercase equivalent:
SELECT * FROM t1
WHERE UPPER(c1) COLLATE Latin1_General_CS_AS <> c1;
This example returns all rows that contain lowercase characters in the c1
column of the t1
table.
We specify a case-sensitive collation to ensure that the comparison is case-sensitive.
Another option is to use the LIKE
operator, while specifying the range of characters we want to match:
SELECT * FROM t1
WHERE c1 LIKE '%[a-z]%'
COLLATE Latin1_General_100_BIN2;
This time I specified a binary collation.
Oracle Database
Oracle’s REGEXP_LIKE
condition complies with the POSIX regular expression standard and the Unicode Regular Expression Guidelines. We can therefore use the [:lower:]
POSIX character class to check for lowercase characters:
SELECT c1 FROM t1
WHERE REGEXP_LIKE(c1, '[[:lower:]]');
Another option is to use the UPPER()
function to compare the original value to its uppercase equivalent:
SELECT c1 FROM t1
WHERE UPPER(c1) <> c1;
MySQL
In MySQL we can use the UPPER()
function to compare the original value to its uppercase equivalent:
SELECT c1 FROM t1
WHERE CAST(UPPER(c1) AS BINARY) <> CAST(c1 AS BINARY);
In this case I cast the values to a binary type in order to effect a case-sensitive match.
It is possible to use the BINARY
operator to do this, however, this operator is deprecated as of MySQL 8.0.27, and it could therefore be removed in a future release.
Another MySQL option is to compare the value to a range of characters:
SELECT c1 FROM t1
WHERE CAST(c1 AS BINARY) REGEXP CAST('[a-z]' AS BINARY);
Again, I cast the values to binary.
MariaDB
MariaDB supports POSIX character classes, so we can use the REGEXP
operator to do this:
SELECT c1 FROM t1
WHERE c1 REGEXP '[[:lower:]]';
Another option is to use the UPPER()
function to compare the original value to its uppercase equivalent:
SELECT c1 FROM t1
WHERE BINARY UPPER(c1) <> BINARY c1;
I use the BINARY
operator to cast the values to binary.
MariaDB hasn’t deprecated its BINARY
operator like MySQL has (at least at the time of writing), so we can use that here.
PostgreSQL
In Postgres, we can use the ~
operator to perform a case sensitive match of a regular expression pattern.
A simple option is to compare our column to the [:lower:]
POSIX character class:
SELECT c1 FROM t1
WHERE c1 ~ '[[:lower:]]';
Another option is to use the UPPER()
function to compare the original value to its uppercase equivalent:
SELECT c1 FROM t1
WHERE UPPER(c1) <> c1;
SQLite
In SQLite we can use the UPPER()
function to compare the original value to its uppercase equivalent:
SELECT c1 FROM t1
WHERE UPPER(c1) <> c1;
SQLite doesn’t fully support unicode characters, and this option won’t return lowercase unicode characters by default. If you need to work with unicode characters, try the SQLite ICU extension.
Another option is to use the REGEXP
operator with a regular expression pattern that includes that range that we want to match:
SELECT c1 FROM t1
WHERE c1 REGEXP '[a-z]';