Sub topic 9
Edge Cases: Position 0, No Match, Zero Length, and NULL
This chapter assumes that you know basic SELECT statements, WHERE clauses, VARCHAR2 values, string literals, the DUAL table, and the basic meaning of NULL in SQL. The main goal is to predict what Oracle returns when a string operation receives an unusual position, length, or argument.
How Oracle treats string positions
Oracle character positions are one-based. The first character is position 1, not 0.
SUBSTR accepts a starting position and an optional length. INSTR returns the position where a search string begins.
| Expression | Result | Reason |
|---|---|---|
| `SUBSTR('abc', 0, 2)` | `ab` | Position `0` is treated as position `1` |
| `SUBSTR('abc', 5)` | `NULL` | The start is past the end |
| `INSTR('abc', 'x')` | `0` | The search string is missing |
| `SUBSTR('abc', 1, 0)` | `NULL` | A length below `1` returns `NULL` |
The following query demonstrates these cases:
SELECT SUBSTR('abc', 0, 2) AS position_zero,
SUBSTR('abc', 5) AS past_end,
SUBSTR('abc', 1, 0) AS zero_length,
SUBSTR('abc', 1, -1) AS negative_length,
INSTR('abc', 'x') AS missing_match
FROM dual;position_zero is ab. past_end, zero_length, and negative_length are NULL. missing_match is 0.
A position of 0 is therefore not a reliable way to represent “before the first character.” Oracle normalizes it to 1. A negative SUBSTR position has a different meaning: it counts backward from the end. However, a negative length is invalid for extraction and produces NULL.
Missing matches and safe parsing
INSTR returns 0 when it cannot find its search string. This differs from NULL, which means that the result is unknown or unavailable.
SELECT INSTR('[email protected]', '@') AS found_at,
INSTR('user.example.com', '@') AS missing_at
FROM dual;The first result is 5; the second is 0. This distinction matters when you use the result as a position:
SELECT CASE
WHEN INSTR(email, '@') > 0
THEN SUBSTR(email, 1, INSTR(email, '@') - 1)
ELSE 'invalid email format'
END AS username
FROM (SELECT 'user.example.com' AS email FROM dual);Without the CASE expression, a missing delimiter can produce an unintended length calculation. Testing for the delimiter first makes the rule clear and prevents malformed input from being treated as valid.
The same pattern applies to names, paths, and delimited codes:
NULL and empty-string arguments
Most character functions return NULL when their main character argument is NULL.
SELECT SUBSTR(CAST(NULL AS VARCHAR2(10)), 1, 2) AS substr_null,
INSTR(CAST(NULL AS VARCHAR2(10)), 'a') AS instr_null,
REPLACE(CAST(NULL AS VARCHAR2(10)), 'a', 'b') AS replace_null
FROM dual;All three results are NULL. The explicit CAST gives the untyped NULL a character type.
Oracle treats '' as NULL for character data. Thus, these expressions have the same effective input:
SELECT LENGTH('') AS empty_length,
LENGTH(CAST(NULL AS VARCHAR2(1))) AS null_length
FROM dual;Both results are NULL, not 0.
REPLACE edge cases
REPLACE returns the original string when the search text is not present. If the replacement argument is omitted or is NULL, Oracle removes each matching occurrence.
SELECT REPLACE('abc', 'x', 'y') AS unchanged,
REPLACE('abc', 'b', NULL) AS removed,
REPLACE('abc', 'b') AS removed_omitted
FROM dual;unchanged is abc. Both removal expressions return ac.
When you nest REPLACE, each call receives the result of the previous call. A missing character remains unless a later call removes it:
SELECT REPLACE(
REPLACE('(555) 123-4567', '(', ''),
')', ''
) AS partially_cleaned
FROM dual;The result is 555 123-4567; the space and hyphen were never targeted.
Exercise
What do INSTR('a,b', ';') and SUBSTR('a,b', 1, INSTR('a,b', ';') - 1) return?
Show answer
INSTR returns 0. The length becomes -1, so SUBSTR returns NULL. Test the delimiter before using its position.