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.
UTF-32:
SELECT max(character_length(user USING CODEUNITS32)) FROM users;
UTF-16:
SELECT max(character_length(user USING CODEUNITS16)) FROM users;
1 byte character set (like ASCII):
SELECT max(character_length(user USING OCTETS)) FROM users;
Reference
Db2 documentation: