Sub topic 7
Splitting Strings by Combining SUBSTR and INSTR
This chapter assumes that you know basic SELECT statements, WHERE clauses, VARCHAR2 values, the DUAL table, and the role of NULL in SQL. The goal is to parse a string by locating a delimiter with INSTR, then extracting text around that delimiter with SUBSTR.
A delimiter is a character or sequence that separates values. In an email address, @ separates the username from the domain. In a comma-separated name, the comma separates the last name from the first name.
The parsing pattern
INSTR returns the position where a delimiter begins. SUBSTR then uses that position as either:
- The end of the text before the delimiter.
- The start of the text after the delimiter.
For example, the @ in [email protected] is at position 5. The username has four characters, so its length is 5 - 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);The result is:
| USERNAME | DOMAIN | |
|---|---|---|
| [email protected] | user | example.com |
The first SUBSTR starts at position 1 and takes all characters before @. The second starts one position after @ and continues to the end because no length is supplied.
Splitting at a comma
The same pattern works for names. In Smith, John, the comma is followed by a space. Add 2 to the comma position to skip both characters.
SELECT full_name,
SUBSTR(full_name, 1, INSTR(full_name, ',') - 1) AS last_name,
SUBSTR(full_name, INSTR(full_name, ',') + 2) AS first_name
FROM (SELECT 'Smith, John' AS full_name FROM dual);The first expression returns Smith. The second returns John.
If spacing is inconsistent, adding a fixed 2 may not be enough. For values such as Smith,John and Smith, John, remove the leading spaces after extraction:
SELECT TRIM(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);TRIM removes spaces at the beginning and end of the extracted value.
Splitting a file name and extension
A file name can contain several dots, so finding the first dot is often wrong. Use the backward-search form of INSTR to find the final dot.
SELECT file_name,
SUBSTR(file_name, 1, INSTR(file_name, '.', -1) - 1) AS base_name,
SUBSTR(file_name, INSTR(file_name, '.', -1) + 1) AS extension
FROM (SELECT 'report.final.pdf' AS file_name FROM dual);The final dot is used as the boundary. The result is report.final and pdf.
For repeated use, calculate the delimiter position once in an inline view. This makes the expressions easier to read and avoids repeating the same search.
SELECT file_name,
SUBSTR(file_name, 1, dot_pos - 1) AS base_name,
SUBSTR(file_name, dot_pos + 1) AS extension
FROM (
SELECT file_name,
INSTR(file_name, '.', -1) AS dot_pos
FROM (SELECT 'report.final.pdf' AS file_name FROM dual)
);Missing delimiters and `NULL`
If INSTR cannot find a delimiter, it returns 0. A calculation such as INSTR(value, '@') - 1 then produces -1, which does not describe a valid split. Check that the delimiter exists before applying the parsing logic.
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);If email is NULL, both INSTR and SUBSTR return NULL. Oracle also treats an empty character string as NULL, so an empty input does not produce a separate empty-string result.
Exercise
How would you extract example.com from [email protected] using SUBSTR and INSTR?
Show answer
Use SUBSTR(email, INSTR(email, '@') + 1). The start position is one character after the at sign.