Sub topic 7
Splitting strings by combining SUBSTR and INSTR
SUBSTR cuts text out of a string, and INSTR tells you where to cut. Used together, they split a value such as an email address, a name, or a file path into parts. This chapter assumes you know how SUBSTR(string, start, length) and INSTR(string, search, start, occurrence) behave on their own, including negative positions. It also assumes you are comfortable with DUAL and with NULL. Here we focus on the patterns that combine them and on how those patterns fail.
The core pattern
Splitting at a delimiter takes three steps:
- Call
INSTRto find the position of the delimiter. Call this positionp. - Take everything before it with
SUBSTR(s, 1, p - 1). - Take everything after it with
SUBSTR(s, p + LENGTH(delimiter)).
The "before" part needs p - 1 as its length because p is the position of the delimiter itself, and you want to stop one character earlier. The "after" part needs an offset equal to the delimiter's length, so the delimiter is skipped. For a one-character delimiter that offset is + 1.
SELECT email,
SUBSTR(email, 1, INSTR(email, '@') - 1) AS username,
SUBSTR(email, INSTR(email, '@') + 1) AS domain
FROM (SELECT '[email protected]' AS email FROM dual);
-- username = 'user', domain = 'example.com'Here INSTR returns 5. The username is SUBSTR(email, 1, 4), which is user. The domain is SUBSTR(email, 6), which is example.com. Because the domain call has no length argument, it runs to the end of the string.
Delimiters longer than one character
A name like Smith, John has a two-character delimiter, a comma followed by a space. Writing + 2 works, but it hides the reason for the 2. Using LENGTH on the delimiter keeps the intent visible and survives a later change of delimiter.
SELECT SUBSTR(full_name, 1, INSTR(full_name, ', ') - 1) AS last_name,
SUBSTR(full_name, INSTR(full_name, ', ') + LENGTH(', ')) AS first_name
FROM (SELECT 'Smith, John' AS full_name FROM dual);
-- last_name = 'Smith', first_name = 'John'The search string is now ', ', so INSTR returns 6, the position of the comma. The last name is SUBSTR(full_name, 1, 5). The first name starts at 6 + 2 = 8.
If the input might have inconsistent spacing, search for the comma alone and wrap the second part in TRIM:
SELECT SUBSTR(full_name, 1, INSTR(full_name, ',') - 1) AS last_name,
TRIM(SUBSTR(full_name, INSTR(full_name, ',') + 1)) AS first_name
FROM (SELECT 'Smith, John' AS full_name FROM dual);
-- last_name = 'Smith', first_name = 'John'Splitting at the last delimiter
File names and paths can contain the delimiter many times, and you usually want the final one. A negative start position makes INSTR search backward from the end, so it returns the position of the last match.
SELECT SUBSTR(f, 1, INSTR(f, '.', -1) - 1) AS base_name,
SUBSTR(f, INSTR(f, '.', -1) + 1) AS extension
FROM (SELECT 'report.final.pdf' AS f FROM dual);
-- base_name = 'report.final', extension = 'pdf'The last dot in report.final.pdf is at position 13. The base name is the first 12 characters, and the extension starts at position 14.
The same idea separates a directory from a file name, using / as the delimiter:
SELECT SUBSTR(p, 1, INSTR(p, '/', -1) - 1) AS directory,
SUBSTR(p, INSTR(p, '/', -1) + 1) AS file_name
FROM (SELECT '/home/ana/reports/q3.csv' AS p FROM dual);
-- directory = '/home/ana/reports', file_name = 'q3.csv'Searching backward still reports positions counted from the left. The value 18 for the last slash here is a normal 1-based position, which is why it can be passed straight into SUBSTR.
Extracting a middle field
When a string has several delimiters, use the fourth argument of INSTR (the occurrence) to find each one. A middle field runs from just after one delimiter to just before the next.
| Part wanted | SUBSTR start | SUBSTR length |
|---|---|---|
| Before the first delimiter | 1 | `p1 - 1` |
| Between the first and second | `p1 + 1` | `p2 - p1 - 1` |
| After the second delimiter | `p2 + 1` | omitted |
In the table, p1 and p2 are the positions of the first and second delimiter, and the delimiter is one character long.
Repeating INSTR calls makes a query hard to read. A common approach is to compute the positions once in a WITH clause and then refer to them by name:
WITH src AS (
SELECT '2026-11-28' AS d FROM dual
),
pos AS (
SELECT d,
INSTR(d, '-', 1, 1) AS p1,
INSTR(d, '-', 1, 2) AS p2
FROM src
)
SELECT SUBSTR(d, 1, p1 - 1) AS yr,
SUBSTR(d, p1 + 1, p2 - p1 - 1) AS mon,
SUBSTR(d, p2 + 1) AS dy
FROM pos;
-- yr = '2026', mon = '11', dy = '28'The dashes are at positions 5 and 8. The month is SUBSTR(d, 6, 2), which is 11. All three results are strings. Wrap them in TO_NUMBER if you need numbers.
For real dates, TO_DATE is the better tool. This pattern is for text that is not a date format Oracle already understands.
Counting occurrences from the end also works. INSTR(s, ',', -1, 2) returns the position of the second-to-last comma.
SELECT INSTR('a,b,c,d', ',', 1, 2) AS second_comma,
INSTR('a,b,c,d', ',', -1, 2) AS second_last_comma
FROM dual;
-- second_comma = 4, second_last_comma = 4In this string both calls return 4 because the commas sit at positions 2, 4, and 6. Counting from the left, the second is 4. Counting from the right, the second is also 4.
When the delimiter is missing
INSTR returns 0 when there is no match. This is where the combined pattern fails, and it fails differently for each half.
- The "before" part computes
SUBSTR(s, 1, 0 - 1), which is a length of -1. A length below 1 returns NULL. - The "after" part computes
SUBSTR(s, 0 + 1), which isSUBSTR(s, 1). That returns the whole string, which looks like valid output but is wrong.
SELECT email,
SUBSTR(email, 1, INSTR(email, '@') - 1) AS username,
SUBSTR(email, INSTR(email, '@') + 1) AS domain
FROM (SELECT 'nodomain' AS email FROM dual);
-- username = NULL, domain = 'nodomain'Nothing raises an error, so bad data passes through quietly. Decide what each case should return, then write that decision into the query. The simplest option is a CASE that returns NULL for both parts:
SELECT email,
CASE WHEN INSTR(email, '@') > 0
THEN SUBSTR(email, 1, INSTR(email, '@') - 1)
END AS username,
CASE WHEN INSTR(email, '@') > 0
THEN SUBSTR(email, INSTR(email, '@') + 1)
END AS domain
FROM (SELECT '[email protected]' AS email FROM dual
UNION ALL
SELECT 'nodomain' FROM dual);
-- row 1: username = 'user', domain = 'example.com'
-- row 2: username = NULL, domain = NULLA CASE with no ELSE returns NULL when the condition is false.
Sometimes you would rather keep the whole string as the first part when no delimiter exists. Appending the delimiter to the string before searching does this:
SELECT SUBSTR(s, 1, INSTR(s || '@', '@') - 1) AS first_part
FROM (SELECT 'nodomain' AS s FROM dual
UNION ALL
SELECT '[email protected]' FROM dual);
-- 'nodomain' and 'user'For nodomain, the search runs on nodomain@, finds the @ at position 9, and returns the first 8 characters. For [email protected], the real @ at position 5 is found first, so the appended one never matters.
NULL and empty input
If the string is NULL, INSTR and SUBSTR both return NULL, so every part is NULL. Oracle treats an empty string as NULL in VARCHAR2, so an empty value behaves the same way. A string that holds only the delimiter, such as '@', is different. The "before" part is SUBSTR('@', 1, 0), which is NULL, and the "after" part is SUBSTR('@', 2), which is also NULL because the start is past the end. The "after" part is NULL in this case, not an empty string. Oracle has no way to represent an empty string value.
Practical checklist
- Compute
INSTRonce per delimiter, either in aWITHclause or an inline view, when a query uses it more than twice. - Use
LENGTH(delimiter)rather than a hard-coded offset when the delimiter is longer than one character. - Use
INSTR(s, d, -1)for the last delimiter andINSTR(s, d, 1, n)for the nth from the left. - Wrap extracted fields in
TRIMif the source may contain stray spaces. - If the split depends on a pattern, not a fixed character (for example "the first run of digits"), a
REGEXP_*function is usually a better fit. A later chapter covers those.
Exercise
Write a query that returns the second field of the string 'alpha;beta;gamma' using only SUBSTR and INSTR. What are the delimiter positions and what does the query return?
Show answer
The semicolons are at positions 6 and 11. The second field starts at 6 + 1 = 7 and has length 11 - 6 - 1 = 4. One solution is: SELECT SUBSTR(s, INSTR(s, ';', 1, 1) + 1, INSTR(s, ';', 1, 2) - INSTR(s, ';', 1, 1) - 1) AS field2 FROM (SELECT 'alpha;beta;gamma' AS s FROM dual); It returns 'beta'.
Exercise
What does SUBSTR('nodomain', 1, INSTR('nodomain', '@') - 1) return, and why?
Show answer
It returns NULL. INSTR returns 0 because there is no '@', so the length argument is 0 - 1 = -1. SUBSTR returns NULL when the length is less than 1.
Exercise
Given the path '/data/in/2026/sales.v2.csv', which expression returns only the file name 'sales.v2.csv'?
Show answer
SUBSTR(p, INSTR(p, '/', -1) + 1). The backward search with a start of -1 finds the last slash, and SUBSTR starts one character after it and runs to the end of the string.