Find the maximum text length in a column

How to get the length of the longest text in a column. Including Db2 specific syntax.

Example: Find the longest username, so we can decide if the column size is big enough.

With MariaDB, MySQL, PostgreSQL, Firebird, RedShift:

SELECT max(char_length(username)) FROM users;

With SQLite, Oracle, Informix, Snowflake:

SELECT max(length(username)) FROM users;

With SQL Server:

SELECT max(len(username)) FROM users;

SQL Server syntax also works with RedShift, Snowflake.

Db2 syntax

In Db2, the syntax to use depends on the character set.


SELECT max(character_length(user USING CODEUNITS32)) FROM users;


SELECT max(character_length(user USING CODEUNITS16)) FROM users;

1 byte character set (like ASCII):

SELECT max(character_length(user USING OCTETS)) FROM users;


Db2 documentation: