Sub topic 10
Using SUBSTR, INSTR, and REPLACE in WHERE, UPDATE, and ORDER BY
This chapter assumes that you know basic SELECT statements, WHERE clauses, VARCHAR2 values, the DUAL table, and the meaning of NULL. The same character functions used in the SELECT list can also filter rows, change stored values, and control sort order.
Filtering rows with character functions
A WHERE clause can test the result of SUBSTR, INSTR, or REPLACE. For example, this query finds email addresses whose domain is example.com:
SELECT customer_id, email
FROM customer_contacts
WHERE SUBSTR(email, INSTR(email, '@') + 1) = 'example.com';INSTR finds the position of @. SUBSTR starts one character after that position and returns the rest of the email address.
You can use INSTR when you only need to test whether text exists:
SELECT customer_id, email
FROM customer_contacts
WHERE INSTR(email, '@') > 0;INSTR returns 0 when it finds no match. Because Oracle treats an empty string as NULL, a NULL email does not satisfy this condition.
REPLACE is useful when the stored format varies. This example finds phone numbers after removing spaces, parentheses, and hyphens:
SELECT customer_id, phone_number
FROM customer_contacts
WHERE REPLACE(
REPLACE(
REPLACE(
REPLACE(phone_number, ' ', ''),
'-', ''),
'(', ''),
')') = '5551234567';Functions in a predicate can make a query easier to write, but they may prevent an ordinary index on the original column from being used efficiently. For frequently searched expressions, consider a function-based index:
CREATE INDEX contacts_phone_clean_ix
ON customer_contacts (
REPLACE(REPLACE(REPLACE(REPLACE(phone_number, ' ', ''), '-', ''), '(', ''), ')', '')
);Use the same expression in the query as in the index definition.
Updating stored text safely
Use these functions in UPDATE expressions when you need to normalize existing data. This statement removes hyphens from phone numbers:
UPDATE customer_contacts
SET phone_number = REPLACE(phone_number, '-', '')
WHERE phone_number IS NOT NULL
AND INSTR(phone_number, '-') > 0;The WHERE clause limits the update to rows that need a change. Always inspect that condition with a SELECT before running the update:
SELECT customer_id, phone_number,
REPLACE(phone_number, '-', '') AS new_phone_number
FROM customer_contacts
WHERE phone_number IS NOT NULL
AND INSTR(phone_number, '-') > 0;You can also extract a value and store it in another column:
UPDATE customer_contacts
SET email_domain = SUBSTR(email, INSTR(email, '@') + 1)
WHERE email IS NOT NULL
AND INSTR(email, '@') > 0;A missing @ would produce an incorrect result, so the condition prevents that case. Review the affected row count and commit only after verification.
Sorting by extracted text
An ORDER BY clause can sort by a character-function expression. To sort contacts by email domain and then username, use:
SELECT customer_id, email,
SUBSTR(email, 1, INSTR(email, '@') - 1) AS username,
SUBSTR(email, INSTR(email, '@') + 1) AS domain
FROM customer_contacts
WHERE email IS NOT NULL
AND INSTR(email, '@') > 0
ORDER BY domain, username;Oracle also allows select-list aliases in ORDER BY, so domain and username can be referenced directly. Sorting by an extracted expression does not change the stored data.
Use NULLS FIRST or NULLS LAST when the position of missing values matters:
SELECT customer_id, email
FROM customer_contacts
ORDER BY SUBSTR(email, INSTR(email, '@') + 1) NULLS LAST;Choosing a practical approach
| Need | Function pattern | Important concern |
|---|---|---|
| Test for a delimiter | `INSTR(column, '@') > 0` | `NULL` values do not match |
| Extract text after a delimiter | `SUBSTR(column, INSTR(column, '@') + 1)` | Validate that the delimiter exists |
| Normalize known characters | Nested `REPLACE` | Every unwanted character needs a replacement |
For a small, fixed set of characters, nested REPLACE calls are clear. For pattern-based extraction or replacement, use REGEXP_SUBSTR, REGEXP_INSTR, or REGEXP_REPLACE. For translating individual characters one at a time, TRANSLATE may be simpler.
Exercise
Why should an update that uses REPLACE usually include a WHERE condition that checks for the unwanted character?
Show answer
The condition avoids rewriting rows that do not need a change and makes the affected-row set easier to inspect before committing.