Skip to content

LENGTH ​

Calculates the length of expr based on characters defined in the input character set.

Syntax ​

sql
LENGTH(expr)

Arguments ​

expr: expr can be of the data types STRING, INTEGER, DECIMAL, or TIMESTAMP.

  • If expr is of the data type STRING, the length includes all leading and trailing spaces.
  • If expr is NULL, NULL is returned.

Return Values ​

The return value is of the data type INTEGER.

Example ​

Without specifying scale.

sql
SELECT 'Gaudeamus' as expr, LENGTH('Gaudeamus') as results 
UNION ALL
SELECT ' Gaudeamus ' as expr, LENGTH(' Gaudeamus ') as results 
UNION ALL
SELECT  TO_CHAR(12345) as expr, LENGTH(12345) as results 
UNION ALL
SELECT  '012345' as expr, LENGTH(012345) as results 
UNION ALL
SELECT  TO_CHAR(012345.23) as expr, LENGTH(012345.23) as results 
UNION ALL
select ' 012345 ' as expr, LENGTH(' 012345 ') as results
UNION ALL 
SELECT 
TO_CHAR(TO_DATE('11-NOV-2021')) as expr,
 LENGTH(TO_DATE('11-NOV-2021')) as results
UNION ALL 
SELECT 
TO_CHAR(TO_DATE('11-11-2021 11:12:13','DD-MM-YYYY HH:MI:SS')) as expr,
LENGTH(TO_DATE('11-11-2021 11:12:13','DD-MM-YYYY HH:MI:SS')) as results
UNION ALL 
SELECT 
TO_CHAR(TO_TIMESTAMP('11-11-2021 11:12:13','DD-MM-YYYY HH:MI:SS')) as expr,
LENGTH(TO_TIMESTAMP('11-11-2021 11:12:13','DD-MM-YYYY HH:MI:SS')) as results;
+---------------------------------+---------+
| EXPR                            | RESULTS |
+---------------------------------+---------+
| Gaudeamus                       | 9       |
|  Gaudeamus                      | 11      |
| 12345                           | 5       |
| 012345                          | 5       |
| 12345.23                        | 8       |
|  012345                         | 8       |
| 11-NOV-21                       | 9       |
| 11-NOV-21                       | 9       |
| 11-NOV-21 11.12.13.000000000 AM | 31      |
+---------------------------------+---------+