Firebird Documentation Index → Firebird 2.1 Language Ref. Update → Internal functions → OVERLAY() |
Available in: DSQL, PSQL
Added in: 2.1
Description: Replaces part of a string with another string. By default, the number of characters removed from the host string equals the length of the replacement string. With the optional fourth argument, the user can specify a different number of characters to be removed.
Result type: VARCHAR or BLOB
Syntax:
OVERLAY (string
PLACINGreplacement
FROMpos
[FORlength
])
This function supports BLOBs of any length. Due to a bug, BLOBs containing multi-byte characters – and sometimes even single-byte non-ASCII characters – will cause a “Cannot transliterate character between character sets” error. This bug does not occur in Firebird 2.5.
If
string
orreplacement
is a BLOB, the result is a BLOB. Otherwise, the result is a VARCHAR(n
) withn
the sum of the lengths ofstring
andreplacement
.As usual in SQL string functions,
pos
is 1-based.If
pos
is beyond the end ofstring
,replacement
is placed directly afterstring.
If the number of characters from
pos
to the end ofstring
is smaller than the length ofreplacement
(or than thelength
argument, if present),string
is truncated atpos
andreplacement
placed after it.The effect of a “FOR 0” clause is that
replacement
is simply inserted intostring
.If any argument is
NULL
, the result isNULL
.If
pos
orlength
is not a whole number, bankers' rounding (round-to-even) is applied, i.e. 0.5 becomes 0, 1.5 becomes 2, 2.5 becomes 2, 3.5 becomes 4, etc.
Examples:
overlay ('Goodbye' placing 'Hello' from 2) -- returns 'GHelloe' overlay ('Goodbye' placing 'Hello' from 5) -- returns 'GoodHello' overlay ('Goodbye' placing 'Hello' from 8) -- returns 'GoodbyeHello' overlay ('Goodbye' placing 'Hello' from 20) -- returns 'GoodbyeHello' overlay ('Goodbye' placing 'Hello' from 2 for 0) -- r. 'GHellooodbye' overlay ('Goodbye' placing 'Hello' from 2 for 3) -- r. 'GHellobye' overlay ('Goodbye' placing 'Hello' from 2 for 6) -- r. 'GHello' overlay ('Goodbye' placing 'Hello' from 2 for 9) -- r. 'GHello' overlay ('Goodbye' placing '' from 4) -- returns 'Goodbye' overlay ('Goodbye' placing '' from 4 for 3) -- returns 'Gooe' overlay ('Goodbye' placing '' from 4 for 20) -- returns 'Goo' overlay ('' placing 'Hello' from 4) -- returns 'Hello' overlay ('' placing 'Hello' from 4 for 0) -- returns 'Hello' overlay ('' placing 'Hello' from 4 for 20) -- returns 'Hello'
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: REPLACE()
Firebird Documentation Index → Firebird 2.1 Language Ref. Update → Internal functions → OVERLAY() |