8.3. String Functions
8.3.1. ASCII_CHAR()
Available inDSQL, PSQL
Possible name conflictYES → Read details
Syntax
ASCII_CHAR (code)
ASCII_CHAR Function Parameter| Parameter | Description |
|---|---|
code | An integer within the range from 0 to 255 |
Result typeCHAR(1) CHARACTER SET NONE
DescriptionReturns the ASCII character corresponding to the number passed in the argument.
If you are used to the behaviour of the
ASCII_CHARUDF, which returns an empty string if the argument is 0, please notice that the internal function correctly returns a character with ASCII code 0 here.
8.3.2. ASCII_VAL()
Available inDSQL, PSQL
Possible name conflictYES → Read details
Syntax
ASCII_VAL (ch)
ASCII_VAL Function Parameter| Parameter | Description |
|---|---|
ch | A string of the |
Result typeSMALLINT
DescriptionReturns the ASCII code of the character passed in.
If the argument is a string with more than one character, the ASCII code of the first character is returned.
If the argument is an empty string, 0 is returned.
If the argument is
NULL,NULLis returned.If the first character of the argument string is multi-byte, an error is raised. (A bug in Firebird 2.1 - 2.1.3 and 2.5.0 causes an error to be raised if any character in the string is multi-byte. This is fixed in versions 2.1.4 and 2.5.1.)
8.3.3. BIT_LENGTH()
Available inDSQL, PSQL
Syntax
BIT_LENGTH (string)
BIT_LENGTH Function Parameter| Parameter | Description |
|---|---|
string | An expression of a string type |
Result typeINTEGER
DescriptionGives the length in bits of the input string.
For multi-byte character sets, this may be less than the number of characters times 8 times the formal
number of bytes per character as found in RDB$CHARACTER_SETS.
With arguments of type CHAR, this function takes the entire formal string length (i.e. the declared length of a field or variable) into account.
If you want to obtain the logical
bit length, not counting the trailing spaces, right-TRIM the argument before passing it to BIT_LENGTH.
BLOB supportSince Firebird 2.1, this function fully supports text BLOBs of any length and character set.
Examples
select bit_length('Hello!') from rdb$database
-- returns 48
select bit_length(_iso8859_1 'Grüß di!') from rdb$database
-- returns 64: ü and ß take up one byte each in ISO8859_1
select bit_length
(cast (_iso8859_1 'Grüß di!' as varchar(24) character set utf8))
from rdb$database
-- returns 80: ü and ß take up two bytes each in UTF8
select bit_length
(cast (_iso8859_1 'Grüß di!' as char(24) character set utf8))
from rdb$database
-- returns 208: all 24 CHAR positions count, and two of them are 16-bit
See alsoSection 8.3.9, “OCTET_LENGTH()”, Section 8.3.4, “CHAR_LENGTH(), CHARACTER_LENGTH()”
8.3.4. CHAR_LENGTH(), CHARACTER_LENGTH()
Available inDSQL, PSQL
Syntax
CHAR_LENGTH (string)
| CHARACTER_LENGTH (string)
CHAR[ACTER]_LENGTH Function Parameter| Parameter | Description |
|---|---|
string | An expression of a string type |
Result typeINTEGER
DescriptionGives the length in characters of the input string.
With arguments of type
CHAR, this function returns the formal string length (i.e. the declared length of a field or variable). If you want to obtain thelogical
length, not counting the trailing spaces, right-TRIMthe argument before passing it toCHAR[ACTER]_LENGTH.BLOBsupport: Since Firebird 2.1, this function fully supports textBLOBs of any length and character set.
Examples
select char_length('Hello!') from rdb$database
-- returns 6
select char_length(_iso8859_1 'Grüß di!') from rdb$database
-- returns 8
select char_length
(cast (_iso8859_1 'Grüß di!' as varchar(24) character set utf8))
from rdb$database
-- returns 8; the fact that ü and ß take up two bytes each is irrelevant
select char_length
(cast (_iso8859_1 'Grüß di!' as char(24) character set utf8))
from rdb$database
-- returns 24: all 24 CHAR positions count
See alsoSection 8.3.3, “BIT_LENGTH()”, Section 8.3.9, “OCTET_LENGTH()”
8.3.5. HASH()
Available inDSQL, PSQL
Syntax
HASH (string)
HASH Function Parameter| Parameter | Description |
|---|---|
string | An expression of a string type |
Result typeBIGINT
DescriptionReturns a hash value for the input string.
This function fully supports text BLOBs of any length and character set.
8.3.6. LEFT()
Available inDSQL, PSQL
Syntax
LEFT (string, length)
LEFT Function Parameters| Parameter | Description |
|---|---|
string | An expression of a string type |
length | Integer expression. Defines the number of characters to return |
Result typeVARCHAR or BLOB
DescriptionReturns the leftmost part of the argument string. The number of characters is given in the second argument.
This function fully supports text
BLOBs of any length, including those with a multi-byte character set.If string is a
BLOB, the result is aBLOB. Otherwise, the result is aVARCHAR(n)with n the length of the input string.If the length argument exceeds the string length, the input string is returned unchanged.
If the length argument 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.
See alsoSection 8.3.14, “RIGHT()”
8.3.7. LOWER()
Available inDSQL, ESQL, PSQL
Possible name conflictYES → Read details below
Syntax
LOWER (string)
LOWER Function ParameterS| Parameter | Description |
|---|---|
string | An expression of a string type |
Result type(VAR)CHAR or BLOB
DescriptionReturns the lower-case equivalent of the input string.
The exact result depends on the character set.
With ASCII or NONE for instance, only ASCII characters are lowercased;
with OCTETS, the entire string is returned unchanged.
Since Firebird 2.1 this function also fully supports text BLOBs of any length and character set.
Because LOWER is a reserved word, the internal function will take precedence even if the external function by that name has also been declared.
To call the (inferior!) external function, use double-quotes and the exact capitalisation, as in "LOWER"(string).
Example
select Sheriff from Towns
where lower(Name) = 'cooper''s valley'
See alsoSection 8.3.18, “UPPER()”
8.3.8. LPAD()
Available inDSQL, PSQL
Possible name conflictYES → Read details
Syntax
LPAD (str, endlen [, padstr])
LPAD Function Parameters| Parameter | Description |
|---|---|
str | An expression of a string type |
endlen | Output string length |
padstr | The character or string to be used to pad the source string up to the specified length.
Default is space ( |
Result typeVARCHAR or BLOB
DescriptionLeft-pads a string with spaces or with a user-supplied string until a given length is reached.
This function fully supports text
BLOBs of any length and character set.If str is a
BLOB, the result is aBLOB. Otherwise, the result is aVARCHAR(endlen).If padstr is given and equals
''(empty string), no padding takes place.If endlen is less than the current string length, the string is truncated to endlen, even if padstr is the empty string.
In Firebird 2.1-2.1.3, all non-BLOB results were of type VARCHAR(32765), which made it advisable to cast them to a more modest size.
This is no longer the case.
When used on a BLOB, this function may need to load the entire object into memory.
Although it does try to limit memory consumption, this may affect performance if huge BLOBs are involved.
Examples
lpad ('Hello', 12) -- returns ' Hello'
lpad ('Hello', 12, '-') -- returns '-------Hello'
lpad ('Hello', 12, '') -- returns 'Hello'
lpad ('Hello', 12, 'abc') -- returns 'abcabcaHello'
lpad ('Hello', 12, 'abcdefghij') -- returns 'abcdefgHello'
lpad ('Hello', 2) -- returns 'He'
lpad ('Hello', 2, '-') -- returns 'He'
lpad ('Hello', 2, '') -- returns 'He'
See alsoSection 8.3.15, “RPAD()”
8.3.9. OCTET_LENGTH()
Available inDSQL, PSQL
Syntax
OCTET_LENGTH (string)
OCTET_LENGTH Function Parameter| Parameter | Description |
|---|---|
string | An expression of a string type |
Result typeINTEGER
DescriptionGives the length in bytes (octets) of the input string.
For multi-byte character sets, this may be less than the number of characters times the formal
number of bytes per character as found in RDB$CHARACTER_SETS.
With arguments of type CHAR, this function takes the entire formal string length (i.e. the declared length of a field or variable) into account.
If you want to obtain the logical
byte length, not counting the trailing spaces, right-TRIM the argument before passing it to OCTET_LENGTH.
BLOB supportSince Firebird 2.1, this function fully supports text BLOBs of any length and character set.
Examples
select octet_length('Hello!') from rdb$database
-- returns 6
select octet_length(_iso8859_1 'Grüß di!') from rdb$database
-- returns 8: ü and ß take up one byte each in ISO8859_1
select octet_length
(cast (_iso8859_1 'Grüß di!' as varchar(24) character set utf8))
from rdb$database
-- returns 10: ü and ß take up two bytes each in UTF8
select octet_length
(cast (_iso8859_1 'Grüß di!' as char(24) character set utf8))
from rdb$database
-- returns 26: all 24 CHAR positions count, and two of them are 2-byte
See alsoSection 8.3.3, “BIT_LENGTH()”, Section 8.3.4, “CHAR_LENGTH(), CHARACTER_LENGTH()”
8.3.10. OVERLAY()
Available inDSQL, PSQL
Syntax
OVERLAY (string PLACING replacement FROM pos [FOR length])
OVERLAY Function Parameters| Parameter | Description |
|---|---|
string | The string into which the replacement takes place |
replacement | Replacement string |
pos | The position from which replacement takes place (starting position) |
length | The number of characters that are to be overwritten |
Result typeVARCHAR or BLOB
DescriptionOVERLAY() overwrites part of a string with another string.
By default, the number of characters removed from (overwritten in) the host string equals the length of the replacement string.
With the optional fourth argument, a different number of characters can be specified for removal.
This function supports
BLOBs of any length.If string or replacement is a
BLOB, the result is aBLOB. Otherwise, the result is aVARCHAR(n)with n the sum of the lengths of string and replacement.As usual in SQL string functions, pos is 1-based.
If pos is beyond the end of string, replacement is placed directly after string.
If the number of characters from pos to the end of string is smaller than the length of replacement (or than the length argument, if present), string is truncated at pos and replacement placed after it.
The effect of a
clause is that replacement is simply inserted into string.FOR 0If any argument is
NULL, the result isNULL.If pos or length 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 alsoSection 8.3.12, “REPLACE()”
8.3.11. POSITION()
Available inDSQL, PSQL
Syntax
POSITION (substr IN string)
| POSITION (substr, string [, startpos])
POSITION Function Parameters| Parameter | Description |
|---|---|
substr | The substring whose position is to be searched for |
string | The string which is to be searched |
startpos | The position in string where the search is to start |
Result typeINTEGER
DescriptionReturns the (1-based) position of the first occurrence of a substring in a host string. With the optional third argument, the search starts at a given offset, disregarding any matches that may occur earlier in the string. If no match is found, the result is 0.
The optional third argument is only supported in the second syntax (comma syntax).
The empty string is considered a substring of every string. Therefore, if substr is
''(empty string) and string is notNULL, the result is:1 if startpos is not given;
startpos if startpos lies within string;
0 if startpos lies beyond the end of string.
Notice: A bug in Firebird 2.1 - 2.1.3 and 2.5.0 causes
POSITIONto always return 1 if substr is the empty string. This is fixed in 2.1.4 and 2.5.1.This function fully supports text
BLOBs of any size and character set.
Examples
position ('be' in 'To be or not to be') -- returns 4
position ('be', 'To be or not to be') -- returns 4
position ('be', 'To be or not to be', 4) -- returns 4
position ('be', 'To be or not to be', 8) -- returns 17
position ('be', 'To be or not to be', 18) -- returns 0
position ('be' in 'Alas, poor Yorick!') -- returns 0
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 alsoSection 8.3.16, “SUBSTRING()”
8.3.12. REPLACE()
Available inDSQL, PSQL
Syntax
REPLACE (str, find, repl)
REPLACE Function Parameters| Parameter | Description |
|---|---|
str | The string in which the replacement is to take place |
find | The string to search for |
repl | The replacement string |
Result typeVARCHAR or BLOB
DescriptionReplaces 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 aBLOB. Otherwise, the result is aVARCHAR(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 alwaysNULL, even if nothing would have been replaced.
Examples
replace ('Billy Wilder', 'il', 'oog') -- returns 'Boogly Woogder'
replace ('Billy Wilder', 'il', '') -- returns 'Bly Wder'
replace ('Billy Wilder', null, 'oog') -- returns NULL
replace ('Billy Wilder', 'il', null) -- returns NULL
replace ('Billy Wilder', 'xyz', null) -- returns NULL (!)
replace ('Billy Wilder', 'xyz', 'abc') -- returns 'Billy Wilder'
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 alsoSection 8.3.10, “OVERLAY()”, Section 8.3.16, “SUBSTRING()”, Section 8.3.11, “POSITION()”, Section 8.3.4, “CHAR_LENGTH(), CHARACTER_LENGTH()”
8.3.13. REVERSE()
Available inDSQL, PSQL
Syntax
REVERSE (string)
REVERSE Function Parameter| Parameter | Description |
|---|---|
string | An expression of a string type |
Result typeVARCHAR
DescriptionReturns a string backwards.
Examples
reverse ('spoonful') -- returns 'lufnoops'
reverse ('Was it a cat I saw?') -- returns '?was I tac a ti saW'
This function comes in very handy if you want to group, search or order on string endings, e.g. when dealing with domain names or email addresses:
create index ix_people_email on peoplecomputed by (reverse(email));select * from peoplewhere reverse(email) starting with reverse('.br');
8.3.14. RIGHT()
Available inDSQL, PSQL
Possible name conflictYES → Read details
Syntax
RIGHT (string, length)
RIGHT Function Parameters| Parameter | Description |
|---|---|
string | An expression of a string type |
length | Integer. Defines the number of characters to return |
Result typeVARCHAR or BLOB
DescriptionReturns the rightmost part of the argument string. The number of characters is given in the second argument.
This function supports text
BLOBs of any length, but has a bug in versions 2.1 - 2.1.3 and 2.5.0 that makes it fail with textBLOBs larger than 1024 bytes that have a multi-byte character set. This has been fixed in versions 2.1.4 and 2.5.1.If string is a
BLOB, the result is aBLOB. Otherwise, the result is aVARCHAR(n)with n the length of the input string.If the length argument exceeds the string length, the input string is returned unchanged.
If the length argument 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.
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 alsoSection 8.3.6, “LEFT()”, Section 8.3.16, “SUBSTRING()”
8.3.15. RPAD()
Available inDSQL, PSQL
Changed in2.5 (backported to 2.1.4)
Possible name conflictYES → Read details
Syntax
RPAD (str, endlen [, padstr])
RPAD Function Parameters| Parameter | Description |
|---|---|
str | An expression of a string type |
endlen | Output string length |
endlen | The character or string to be used to pad the source string up to the specified length.
Default is space ( |
Result typeVARCHAR or BLOB
DescriptionRight-pads a string with spaces or with a user-supplied string until a given length is reached.
This function fully supports text
BLOBs of any length and character set.If str is a
BLOB, the result is aBLOB. Otherwise, the result is aVARCHAR(endlen).If padstr is given and equals
''(empty string), no padding takes place.If endlen is less than the current string length, the string is truncated to endlen, even if padstr is the empty string.
In Firebird 2.1-2.1.3, all non-BLOB results were of type VARCHAR(32765), which made it advisable to cast them to a more modest size.
This is no longer the case.
Examples
rpad ('Hello', 12) -- returns 'Hello '
rpad ('Hello', 12, '-') -- returns 'Hello-------'
rpad ('Hello', 12, '') -- returns 'Hello'
rpad ('Hello', 12, 'abc') -- returns 'Helloabcabca'
rpad ('Hello', 12, 'abcdefghij') -- returns 'Helloabcdefg'
rpad ('Hello', 2) -- returns 'He'
rpad ('Hello', 2, '-') -- returns 'He'
rpad ('Hello', 2, '') -- returns 'He'
When used on a BLOB, this function may need to load the entire object into memory.
Although it does try to limit memory consumption, this may affect performance if huge BLOBs are involved.
See alsoSection 8.3.8, “LPAD()”
8.3.16. SUBSTRING()
Available inDSQL, PSQL
Changed in2.5.1
Syntax
SUBSTRING (str FROM startpos [FOR length])
SUBSTRING Function Parameters| Parameter | Description |
|---|---|
str | An expression of a string type |
startpos | Integer expression, the position from which to start retrieving the substring |
length | The number of characters to retrieve after the startpos |
Result typesVARCHAR or BLOB
DescriptionReturns a string’s substring starting at the given position, either to the end of the string or with a given length.
This function returns the substring starting at character position startpos (the first position being 1).
Without the FOR argument, it returns all the remaining characters in the string.
With FOR, it returns length characters or the remainder of the string, whichever is shorter.
In Firebird 1.x, startpos and length must be integer literals. In 2.0 and above they can be any valid integer expression.
Starting with Firebird 2.1, this function fully supports binary and text BLOBs of any length and character set.
If str is a BLOB, the result is also a BLOB.
For any other argument type, the result is a VARCHAR.
Previously, the result type used to be CHAR if the argument was a CHAR or a string literal.
For non-BLOB arguments, the width of the result field is always equal to the length of str, regardless of startpos and length.
So, substring('pinhead' from 4 for 2) will return a VARCHAR(7) containing the string 'he'.
If any argument is NULL, the result is NULL.
If str is a
BLOBand the length argument is not present, the output is limited to 32767 characters. Workaround: with longBLOBs, always specifychar_length(str)— or a sufficiently high integer — as the third argument, unless you are sure that the requested substring fits within 32767 characters.This bug has been fixed in version 2.5.1; the fix was also backported to 2.1.5.
An older bug in Firebird 2.0, which caused the function to return
false emptystrings
if startpos or length wasNULL, was fixed.
Example
insert into AbbrNames(AbbrName)
select substring(LongName from 1 for 3) from LongNames
When used on a BLOB, this function may need to load the entire object into memory.
Although it does try to limit memory consumption, this may affect performance if huge BLOBs are involved.
See alsoSection 8.3.11, “POSITION()”, Section 8.3.6, “LEFT()”, Section 8.3.14, “RIGHT()”, Section 8.3.4, “CHAR_LENGTH(), CHARACTER_LENGTH()”
8.3.17. TRIM()
Available inDSQL, PSQL
Syntax
TRIM ([<adjust>] str)
<adjust> ::= {[<where>] [what]} FROM
<where> ::= BOTH | LEADING | TRAILING
TRIM Function Parameters| Parameter | Description |
|---|---|
str | An expression of a string type |
where | The position the substring is to be removed from — |
what | The substring that should be removed (multiple times if there are several matches) from the beginning, the end, or both sides of the input string str.
By default it is space ( |
Result typeVARCHAR or BLOB
DescriptionRemoves leading and/or trailing spaces (or optionally other strings) from the input string.
Since Firebird 2.1 this function fully supports text BLOBs of any length and character set.
Examples
select trim (' Waste no space ') from rdb$database
-- returns 'Waste no space'
select trim (leading from ' Waste no space ') from rdb$database
-- returns 'Waste no space '
select trim (leading '.' from ' Waste no space ') from rdb$database
-- returns ' Waste no space '
select trim (trailing '!' from 'Help!!!!') from rdb$database
-- returns 'Help'
select trim ('la' from 'lalala I love you Ella') from rdb$database
-- returns ' I love you El'
select trim ('la' from 'Lalala I love you Ella') from rdb$database
-- returns 'Lalala I love you El'
If str is a
BLOB, the result is aBLOB. Otherwise, it is aVARCHAR(n)with n the formal length of str.The substring to be removed, if specified, may not be bigger than 32767 bytes. However, if this substring is repeated at str's head or tail, the total number of bytes removed may be far greater. (The restriction on the size of the substring will be lifted in Firebird 3.)
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.
8.3.18. UPPER()
Available inDSQL, ESQL, PSQL
Syntax
UPPER (str)
UPPER Function Parameter| Parameter | Description |
|---|---|
str | An expression of a string type |
Result type(VAR)CHAR or BLOB
DescriptionReturns the upper-case equivalent of the input string.
The exact result depends on the character set.
With ASCII or NONE for instance, only ASCII characters are uppercased;
with OCTETS, the entire string is returned unchanged.
Since Firebird 2.1 this function also fully supports text BLOBs of any length and character set.
Examples
select upper(_iso8859_1 'Débâcle')
from rdb$database
-- returns 'DÉBÂCLE' (before Firebird 2.0: 'DéBâCLE')
select upper(_iso8859_1 'Débâcle' collate fr_fr)
from rdb$database
-- returns 'DEBACLE', following French uppercasing rules
See alsoSection 8.3.7, “LOWER()”