Sub topic 12
Byte and character semantics: SUBSTRB, SUBSTRC, INSTRB
Assume you know basic SELECT statements, VARCHAR2, string literals, DUAL, and the difference between NULL and a non-NULL value. This chapter focuses on a less visible issue: Oracle can count a string in bytes or in characters.
Why counting units matters
A character is a displayed text unit such as A, é, or 漢. A byte is a smaller storage unit used to encode that character. In a multibyte database character set, one character can occupy several bytes.
The ordinary functions use character semantics:
SUBSTRextracts by character position.INSTRreturns a character position.LENGTHreturns a character count.
Their byte-oriented variants are:
SUBSTRB, which extracts by byte position and byte length.INSTRB, which returns a byte position.LENGTHB, which returns a byte count.
SUBSTRC counts Unicode complete characters. It is useful when you want an explicitly character-oriented operation that treats a complete Unicode character as one unit.
SELECT 'é' AS text_value,
LENGTH('é') AS character_count,
LENGTHB('é') AS byte_count,
LENGTHC('é') AS complete_character_count
FROM dual;The byte count depends on the database character set. In a UTF-8 database, é commonly occupies two bytes. The exact result should therefore be checked in the target database rather than assumed.
Choosing the function
Use character semantics for normal text processing. Use byte semantics only when a specification, file format, protocol, or legacy database interface defines limits in bytes.
SELECT SUBSTR('Aé漢B', 1, 3) AS characters,
SUBSTRB('Aé漢B', 1, 3) AS bytes,
SUBSTRC('Aé漢B', 1, 3) AS complete_characters
FROM dual;SUBSTR and SUBSTRC request three character units. SUBSTRB requests three bytes. If the database uses a multibyte encoding, those three bytes may not represent the same visible text as three characters. A byte boundary can fall inside the encoding of a multibyte character.
SUBSTRC is not a way to preserve a fixed byte limit. It preserves complete Unicode characters, so its result can use more bytes than the requested count.
INSTRB and delimiter positions
INSTRB searches for a substring but reports the starting position in bytes. INSTR reports the starting position in characters.
SELECT INSTR('Aé/B', '/') AS character_position,
INSTRB('Aé/B', '/') AS byte_position
FROM dual;The slash is the third character. In a UTF-8 database, é may use two bytes, so the slash begins at byte position four. This difference matters when you combine the result with another function. A character position from INSTR belongs with SUBSTR; a byte position from INSTRB belongs with SUBSTRB.
Do not mix the two units:
SELECT SUBSTRB(path_text, 1, INSTR(path_text, '/') - 1) AS unsafe_prefix
FROM (SELECT 'Aé/B' AS path_text FROM dual);The length calculated by INSTR is in characters, but SUBSTRB interprets it as bytes. That expression can cut the value at the wrong location.
Limits and common errors
A byte limit is often used for fixed-width exports or an external system with a maximum record size. Truncating blindly can produce invalid or damaged text. Prefer character-safe truncation unless the receiving system explicitly requires byte truncation.
Oracle also provides SUBSTR2, SUBSTR4, INSTR2, and INSTR4 for UCS2 and UCS4 code-point semantics. These are specialized choices. They are different from both database-character semantics and raw byte semantics, so use them only when the external specification names that unit.
SUBSTRB, SUBSTRC, and related functions have datatype restrictions in Oracle versions and configurations. In particular, character-complete variants are not general replacements for processing every large-object datatype. Check the SQL Reference for the exact datatype rules of your Oracle release.
Practical decision table
| Requirement | Prefer | Reason |
|---|---|---|
| Split an email address or file path | `SUBSTR` and `INSTR` | Positions match normal character indexing |
| Enforce a byte-oriented interface limit | `SUBSTRB` and `LENGTHB` | Both functions use bytes |
| Preserve complete Unicode characters | `SUBSTRC` | The extraction uses complete characters |
| Find a delimiter for ordinary text | `INSTR` | Its result can be passed to `SUBSTR` |
Exercise
In a multibyte database, why should you avoid using INSTR(pathtext, '/') as the length argument for SUBSTRB(pathtext, 1, ...)?
Show answer
INSTR returns a character position, but SUBSTRB expects a byte length. The units can differ, so the result may end at the wrong byte boundary or cut a multibyte character.