Sub topic 1
Working with strings in Oracle: positions and NULLs
Oracle character functions work with text values such as VARCHAR2 and character literals. This chapter assumes that you already know basic SELECT statements, WHERE clauses, string literals, the DUAL table, and the basic meaning of NULL.
Prerequisites
You should be able to:
- Write a
SELECTstatement. - Read and create
VARCHAR2values. - Use a string literal such as
'Oracle'. - Explain that
NULLmeans an unknown or missing value.
The position model
Oracle's character functions use positions that start at 1, not 0. The first character is at position 1, the second is at position 2, and so on.
SELECT SUBSTR('Oracle', 1, 2) AS first_two,
INSTR('Oracle', 'c') AS c_position
FROM dual;The result is Or and 4. SUBSTR extracts characters, while INSTR finds where a substring begins.
For ordinary character data, these functions count characters. They do not normally count the spaces between words as special. A space is simply another character.
| Text | Position 1 | Position 2 | Position 3 | Position 4 |
|---|---|---|---|---|
| `Code` | `C` | `o` | `d` | `e` |
A function receives a NULL argument when a value is missing. In most expressions, a NULL input produces a NULL result. This behavior is different from an empty string in many database systems.
How Oracle treats empty strings
In Oracle SQL, an empty character literal, written as '', is treated as NULL for VARCHAR2 and other ordinary character expressions. Therefore, these two expressions produce NULL:
SELECT LENGTH('') AS empty_length,
LENGTH(NULL) AS null_length
FROM dual;LENGTH returns NULL in both cases. You should not use = NULL to test for a missing value. Use IS NULL instead:
SELECT employee_id, email
FROM employees
WHERE email IS NULL;A comparison such as email = '' does not find empty VARCHAR2 values because Oracle treats the empty string as NULL, and NULL is not equal to anything.
Choosing the right function
Use the functions according to the question you need to answer:
- Use
SUBSTRwhen you know the location and want to extract text. - Use
INSTRwhen you need to locate a character or substring. - Use
REPLACEwhen you want to substitute every occurrence of text or remove it.
The functions can be combined. INSTR can find a delimiter, and SUBSTR can use that position to extract a part of the value.
Common assumptions and errors
A frequent error is treating positions as zero-based. In Oracle, position 0 is handled as position 1 by SUBSTR, but relying on that special case makes SQL harder to read. Use 1 explicitly.
Another error is confusing a missing match with a NULL result. INSTR returns 0 when it does not find its search string. It returns NULL when an input argument is NULL.
SELECT INSTR('Oracle', 'x') AS missing_position,
INSTR(NULL, 'x') AS null_input
FROM dual;The first result is 0; the second is NULL. The distinction matters when you use the result in calculations or conditions.
REPLACE returns the source text when it finds no match. If the source text or search text is NULL, the result follows Oracle's NULL rules. If you omit the replacement text, Oracle removes each matching occurrence.
SELECT REPLACE('A-B-C', '-', '/') AS changed,
REPLACE('A-B-C', '-') AS removed
FROM dual;The results are A/B/C and ABC.
Understanding these rules gives you a reliable foundation for extracting fields, locating delimiters, and cleaning text. The next step is to control exactly how many characters each function processes.
Exercise
What do INSTR('abc', 'x') and SUBSTR('abc', 1, 0) return in Oracle?
Show answer
INSTR returns 0 because x is absent. SUBSTR returns NULL because a length below 1 produces NULL.