8.3.12. REPLACE()

Available in

DSQL, PSQL

Syntax

  1. REPLACE (str, find, repl)
Table 134. REPLACE Function Parameters
ParameterDescription

str

The string in which the replacement is to take place

find

The string to search for

repl

The replacement string

Result type

VARCHAR or BLOB

Description

Replaces all occurrences of a substring in a string.

  • This function fully supports text BLOBs of any length and character set.

  • If any argument is a BLOB, the result is a BLOB. Otherwise, the result is a VARCHAR(*n*) with n calculated from the lengths of str, find and repl in such a way that even the maximum possible number of replacements won’t overflow the field.

  • If find is the empty string, str is returned unchanged.

  • If repl is the empty string, all occurrences of find are deleted from str.

  • If any argument is NULL, the result is always NULL, even if nothing would have been replaced.

Examples

  1. replace ('Billy Wilder', 'il', 'oog') -- returns 'Boogly Woogder'
  2. replace ('Billy Wilder', 'il', '') -- returns 'Bly Wder'
  3. replace ('Billy Wilder', null, 'oog') -- returns NULL
  4. replace ('Billy Wilder', 'il', null) -- returns NULL
  5. replace ('Billy Wilder', 'xyz', null) -- returns NULL (!)
  6. replace ('Billy Wilder', 'xyz', 'abc') -- returns 'Billy Wilder'
  7. replace ('Billy Wilder', '', 'abc') -- returns 'Billy Wilder'

When used on a BLOB, this function may need to load the entire object into memory. This may affect performance if huge BLOBs are involved.

See also

OVERLAY(), SUBSTRING(), POSITION(), CHAR_LENGTH(), CHARACTER_LENGTH()