Sub topic 6
INSTR with a start position, an occurrence, and backward search
This chapter assumes you already know the two-argument form of INSTR and how SUBSTR takes a start position and length. Here you will use INSTR's third and fourth arguments to find the nth delimiter, search from the end of a string, and extract fields from the middle of a string.
The full signature
INSTR(string, substring [, position [, occurrence]])| Argument | Default | Meaning |
|---|---|---|
| `position` | 1 | Where the search begins. A positive value searches forward. A negative value counts back from the end of the string and searches backward. |
| `occurrence` | 1 | Which match to return, counting in the direction of the search. It must be 1 or greater. |
The return value is always the 1-based position of the first character of the match, counted from the left end of the string. This holds in both directions. A backward search never returns a negative number or a position counted from the right. It returns 0 when there is no match.
Forward search with a start position
The position argument tells INSTR where to begin looking. The character at that position is included in the search.
SELECT INSTR('a,b,c,d', ',', 3) AS from_3,
INSTR('a,b,c,d', ',', 4) AS from_4,
INSTR('a,b,c,d', ',', 5) AS from_5
FROM dual;
-- from_3 = 4, from_4 = 4, from_5 = 6The string a,b,c,d has commas at positions 2, 4, and 6. Starting at 3 skips the first comma, so the search finds the one at 4. Starting at 4 still finds that comma, because the start position is inclusive. Starting at 5 skips it.
A common pattern finds the second delimiter by starting just after the first:
SELECT INSTR('28/11/2026', '/', INSTR('28/11/2026', '/') + 1) AS second_slash
FROM dual;
-- second_slash = 6The inner INSTR returns 3. The outer one starts at 4 and finds the slash at 6. This works, but it gets hard to read with a third or fourth delimiter. The occurrence argument does the same job directly.
Choosing an occurrence
The fourth argument selects which match to return.
SELECT INSTR('a,b,c,d', ',', 1, 1) AS first_comma,
INSTR('a,b,c,d', ',', 1, 2) AS second_comma,
INSTR('a,b,c,d', ',', 1, 3) AS third_comma,
INSTR('a,b,c,d', ',', 1, 4) AS fourth_comma
FROM dual;
-- first_comma = 2, second_comma = 4, third_comma = 6, fourth_comma = 0There are only three commas, so asking for the fourth returns 0. A missing occurrence is not an error.
The start position and the occurrence combine. Occurrences are counted from the start position, not from the beginning of the string. This example comes from the Oracle documentation:
SELECT INSTR('CORPORATE FLOOR', 'OR', 3, 2) AS second_or_from_3
FROM dual;
-- second_or_from_3 = 14Searching from position 3, the first OR starts at 5 and the second at 14. The OR at position 2 is before the start position, so it is never counted.
The substring can be longer than one character. The returned position is where the match begins.
Backward search with a negative position
A negative position is counted from the end of the string. -1 means the last character, -2 the second to last, and so on. INSTR then searches leftward from that point.
SELECT INSTR('a,b,c,d', ',', -1) AS last_comma,
INSTR('a,b,c,d', ',', -1, 2) AS second_last,
INSTR('a,b,c,d', ',', -1, 3) AS third_last,
INSTR('a,b,c,d', ',', -1, 4) AS fourth_last
FROM dual;
-- last_comma = 6, second_last = 4, third_last = 2, fourth_last = 0Notice that occurrence counts in the direction of the search. With a negative position, occurrence 1 is the match closest to the end, and occurrence 2 is the next one to its left.
A negative position -n in a string of length L corresponds to forward position L − n + 1. The table below shows the search start for a,b,c,d, which has length 7.
| `position` | Start of search | First comma found searching backward |
|---|---|---|
| -1 | 7 (the `d`) | 6 |
| -2 | 6 (the last comma) | 6 |
| -3 | 5 (the `c`) | 4 |
| -6 | 2 (the first comma) | 2 |
| -7 | 1 (the `a`) | 0 |
When the search string is longer than one character, a backward search looks for matches whose first character is at or before the start position. The documented example is INSTR('CORPORATE FLOOR', 'OR', -3, 2), which returns 2. The search starts at position 13 and moves left. The OR at 5 is the first match and the OR at 2 is the second.
Parsing with the nth delimiter
Backward search makes "everything after the last delimiter" simple, which is the usual way to get a file name or extension.
WITH t AS (SELECT '/u01/app/oracle/data/file.dbf' AS path FROM dual)
SELECT SUBSTR(path, INSTR(path, '/', -1) + 1) AS file_name,
SUBSTR(path, 1, INSTR(path, '/', -1) - 1) AS directory
FROM t;
-- file_name = 'file.dbf', directory = '/u01/app/oracle/data'The last slash is at position 21. The file name starts at 22. The directory is the 20 characters before the slash.
Combining the last and second-to-last slash gives the name of the parent directory:
WITH t AS (SELECT '/u01/app/oracle/data/file.dbf' AS path FROM dual)
SELECT SUBSTR(path,
INSTR(path, '/', -1, 2) + 1,
INSTR(path, '/', -1) - INSTR(path, '/', -1, 2) - 1) AS parent_dir
FROM t;
-- parent_dir = 'data'The slashes sit at 16 and 21. The substring starts at 17 and has length 21 − 16 − 1 = 4.
Extracting the nth field
For the nth field of a delimited list, take the text between occurrence n − 1 and occurrence n of the delimiter. The general formula has two problems. The first field has no delimiter before it, and the last field has none after it. Padding the string with a delimiter at both ends removes both problems.
WITH data AS (SELECT 'a,b,c,d' AS s, 3 AS n FROM dual),
padded AS (SELECT ',' || s || ',' AS t, n FROM data)
SELECT SUBSTR(t,
INSTR(t, ',', 1, n) + 1,
INSTR(t, ',', 1, n + 1) - INSTR(t, ',', 1, n) - 1) AS field_n
FROM padded;
-- field_n = 'c'The padded string is ,a,b,c,d,. For n = 3 the third comma is at 6 and the fourth at 7, so the field starts at 7 − 1 = 6 and has length 1. Change n to 1 or 4 and the same query returns a or d.
Why the padding matters
Without padding, the last field fails. Here is the third field of a,b,c with no trailing comma:
SELECT SUBSTR('a,b,c',
INSTR('a,b,c', ',', 1, 2) + 1,
INSTR('a,b,c', ',', 1, 3) - INSTR('a,b,c', ',', 1, 2) - 1) AS broken
FROM dual;
-- broken = NULLThere is no third comma, so INSTR returns 0. The length becomes 0 − 4 − 1 = −5, and SUBSTR returns NULL for a length below 1. This is the most common mistake in field extraction.
Edge cases specific to INSTR
| Call | Result | Reason |
|---|---|---|
| `INSTR('abc', 'z')` | 0 | No match. |
| `INSTR('abc', 'a', 10)` | 0 | The start is past the end. |
| `INSTR('abc', 'a', -10)` | 0 | A negative start before the beginning. |
| `INSTR('abc', 'a', 0)` | 0 | Position 0 is not a valid start. Unlike SUBSTR, INSTR does not treat it as 1. |
| `INSTR('Abc', 'a')` | 0 | Matching is case sensitive. |
| `INSTR('abc', NULL)` | NULL | Any NULL argument gives NULL. |
| `INSTR('abc', '')` | NULL | Oracle treats an empty string as NULL. |
| `INSTR('a,b', ',', 1, 0)` | error | An occurrence below 1 raises ORA-01428. |
Two of these deserve attention.
- Position 0 is different from SUBSTR. SUBSTR treats a start of 0 as 1. INSTR returns 0 for a position of 0. If you compute the start position from another INSTR that can return 0, check what you are passing.
- A 0 result is valid data, not an error. Code that adds 1 to a failed INSTR gets position 1 and silently returns the whole string. For example,
SUBSTR(email, INSTR(email, '@') + 1)returns the entire value when there is no@. If that matters, guard it:
SELECT email,
CASE WHEN INSTR(email, '@') > 0
THEN SUBSTR(email, INSTR(email, '@') + 1)
END AS domain
FROM (SELECT 'no-at-sign' AS email FROM dual);
-- domain = NULLChoosing between forward and backward search
Searching from the end can be faster and clearer than counting from the start, but the two directions answer different questions. These rules cover most cases.
- Use a backward search when the part you want is anchored to the end, such as the extension, the file name, or the last word.
- Use a forward search with an occurrence when the part is anchored to the start, such as the second field.
- Remember that a backward search returns a position counted from the left. You can pass it straight to SUBSTR with no conversion.
- Pass a literal
1as the start when you only need theoccurrenceargument. The occurrence cannot be given without the position.
Exercises
Exercise
What do these two queries return? SELECT INSTR('one two one two', 'one', 2) AS q1, INSTR('one two one two', 'two', -1, 2) AS q2 FROM dual;
Show answer
q1 = 9 and q2 = 5. The string has one at 1 and 9, and two at 5 and 13. Starting at position 2 skips the one at 1, so the first match is at 9. Searching backward from the end for two, occurrence 1 is at 13 and occurrence 2 is at 5. Both results are positions counted from the left.
Exercise
Given the value 'archive.2026.11.tar.gz', write a query that returns 'tar.gz', meaning everything after the second-to-last dot. Which INSTR call finds the right dot, and what would the query return if the value had no dots at all?
Show answer
SELECT SUBSTR(f, INSTR(f, '.', -1, 2) + 1) AS ext FROM (SELECT 'archive.2026.11.tar.gz' AS f FROM dual); The call INSTR(f, '.', -1, 2) searches backward and returns the second dot from the right, which is at position 16. SUBSTR then starts at 17, giving 'tar.gz'. If the value had no dots, INSTR would return 0, and SUBSTR(f, 0 + 1) would return the whole string, so a guard such as CASE WHEN INSTR(f, '.', -1, 2) > 0 is needed when that case is possible.