LENGTHC Function
1. Purpose
LENGTHC is an Oracle string-length function that counts Unicode complete characters. In a UTF8 database, IvorySQL counts code points after NFC normalization, so a precomposed character and its canonically equivalent decomposed form have the same length.
2. Functionality
Use LENGTHC to compare text lengths after normalization. An accented letter, for example, can be represented by one code point or by a letter followed by a combining accent.
The ivorysql_ora extension provides LENGTHC. The examples connect to a UTF8 database through the Oracle compatibility port, which defaults to 1521.
2.1. Interface
LENGTHC(value) returns an integer. You can also call it as sys.lengthc(value).
The argument can be a SQL expression such as a literal, table column, function result, or scalar subquery. Its type must match a supported overload directly or through an available type conversion.
A character input of NULL returns NULL. An empty string returns NULL with the default ivorysql.enable_emptystring_to_NULL = on, or zero with off. Trailing padding spaces in CHAR values count toward the length.
2.2. Counting Units in Oracle Length Functions
| Function | Unit |
|---|---|
|
Characters in the input character set |
|
Bytes |
|
Unicode complete characters |
|
UCS-2 code units |
|
UCS-4 code units |
IvorySQL counts code points after NFC normalization in UTF8 databases. In other database encodings, LENGTHC remains available and counts characters directly in that encoding.
2.3. Oracle Compatibility Scope
IvorySQL accepts character inputs such as CHAR, NCHAR, VARCHAR2, and text. Oracle also lists NVARCHAR2, a type name that IvorySQL currently does not recognize. Direct CLOB, NCLOB, and BLOB inputs raise a type error, including NULL of those types. Oracle LENGTH Function Reference
Numeric and datetime values and binary inputs such as RAW and bytea are converted to text before counting. Datetime results depend on the session format. For binary inputs, the default hexadecimal format includes the \x prefix in the count.
3. Examples
3.1. Compare Character, Byte, and Normalized Lengths
SELECT sample_name,
pg_catalog.encode(pg_catalog.convert_to(sample_text, 'UTF8'), 'hex') AS utf8_hex,
LENGTH(sample_text) AS char_count,
LENGTHB(sample_text) AS byte_count,
LENGTHC(sample_text) AS normalized_count
FROM (VALUES (1, 'precomposed', CHR(233)),
(2, 'decomposed', 'e' || CHR(769)),
(3, 'q_with_accent', 'q' || CHR(769))) samples(sample_no, sample_name, sample_text)
ORDER BY sample_no;
sample_name | utf8_hex | char_count | byte_count | normalized_count ---------------+----------+------------+------------+------------------ precomposed | c3a9 | 1 | 2 | 1 decomposed | 65cc81 | 2 | 3 | 1 q_with_accent | 71cc81 | 2 | 3 | 2 (3 rows)
Precomposed é uses one code point, U+00E9. The decomposed form uses e (U+0065) and a combining acute accent (U+0301). NFC normalizes both forms to the same code point. Unicode Normalization Forms
|
NFC applies the composition rules defined by Unicode. A |
3.2. Count a Fixed-Length Character Value
SELECT LENGTHC(CAST('ab' AS CHAR(5 CHAR))) AS fixed_length,
LENGTHC(CAST('ab' AS VARCHAR2(5 CHAR))) AS variable_length
FROM dual;
fixed_length | variable_length
--------------+-----------------
5 | 2
(1 row)
fixed_length is 5, including three padding spaces. variable_length is 2.