8.5. Type Casting Functions
DSQL, ESQL, PSQL
Changed in
2.5
Syntax
Result type
User-chosen.
CAST
converts an expression to the desired datatype or domain. If the conversion is not possible, an error is raised.
Alternative syntax, supported only when casting a string literal to a DATE
, TIME
or TIMESTAMP
:
datatype 'date/timestring'
This syntax was already available in InterBase, but was never properly documented. In the SQL standard, this feature is called “datetime literals”.
Cast Examples
A full-syntax cast:
select cast ('12' || '-June-' || '1959' as date) from rdb$database
A shorthand string-to-date cast:
Notice that you can drop even the shorthand cast from the example above, as the engine will understand from the context (comparison to a DATE
field) how to interpret the string:
update People set AgeCat = 'Old'
where BirthDate < '1-Jan-1943'
But this is not always possible. The cast below cannot be dropped, otherwise the engine would find itself with an integer to be subtracted from a string:
The following table shows the type conversions possible with CAST
.
Keep in mind that sometimes information is lost, for instance when you cast a TIMESTAMP
to a DATE
. Also, the fact that types are CAST
-compatible is in itself no guarantee that a conversion will succeed. “CAST(123456789 as SMALLINT)
” will definitely result in an error, as will “CAST('Judgement Day' as DATE)
”.
Casting input fields
Since Firebird 2.0, you can cast statement parameters to a datatype:
This gives you control over the type of input field set up by the engine. Please notice that with statement parameters, you always need a full-syntax cast — shorthand casts are not supported.
Casting to a domain or its type
Firebird 2.1 and above support casting to a domain or its base type. When casting to a domain, any constraints (NOT NULL
and/or CHECK
) declared for the domain must be satisfied or the cast will fail. Please be aware that a CHECK
passes if it evaluates to or NULL
! So, given the following statements:
create domain quint as int check (value >= 5000);
select cast (2000 as quint) from rdb$database; (1)
select cast (8000 as quint) from rdb$database; (2)
select cast (null as quint) from rdb$database; (3)
only cast number 1 will result in an error.
When the TYPE OF
modifier is used, the expression is cast to the base type of the domain, ignoring any constraints. With domain quint
defined as above, the following two casts are equivalent and will both succeed:
select cast (2000 as type of quint) from rdb$database;
select cast (2000 as int) from rdb$database;
If TYPE OF
is used with a (VAR)CHAR
type, its character set and collation are retained:
Casting to a column’s type
In Firebird 2.5 and above, it is possible to cast expressions to the type of an existing table or view column. Only the type itself is used; in the case of string types, this includes the character set but not the collation. Constraints and default values of the source column are not applied.
create table ttt (
);
commit;
select cast ('Jag har många vänner' as type of column ttt.s)
from rdb$database;
Casting BLOB
s
Successful casting to and from s is possible since Firebird 2.1.