Find Rows that Contain Uppercase Characters in SQL

Most RDBMSs provide at least a few ways to return rows that contain uppercase characters. Here are some options available in the major RDBMSs.

SQL Server

In SQL Server, we can use the LOWER() function to compare the original value to its lowercase equivalent:

SELECT * FROM t1
WHERE LOWER(c1) COLLATE Latin1_General_CS_AS <> c1;

This example returns all rows that contain uppercase 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 [:upper:] POSIX character class to check for uppercase characters:

SELECT c1 FROM t1
WHERE REGEXP_LIKE(c1, '[[:upper:]]');

Another option is to use the LOWER() function to compare the original value to its lowercase equivalent:

SELECT c1 FROM t1
WHERE LOWER(c1) <> c1;

MySQL

In MySQL we can use the LOWER() function to compare the original value to its lowercase equivalent:

SELECT c1 FROM t1
WHERE CAST(LOWER(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 '[[:upper:]]';

Another option is to use the LOWER() function to compare the original value to its uppercase equivalent:

SELECT c1 FROM t1
WHERE BINARY LOWER(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 [:upper:] POSIX character class:

SELECT c1 FROM t1
WHERE c1 ~ '[[:upper:]]';

Another option is to use the LOWER() function to compare the original value to its lowercase equivalent:

SELECT c1 FROM t1
WHERE LOWER(c1) <> c1;

SQLite

In SQLite we can use the LOWER() function to compare the original value to its lowercase equivalent:

SELECT c1 FROM t1
WHERE LOWER(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]';