8.5 Type Casting Functions

8.5.1 CAST()

Available inDSQL, ESQL, PSQL

Result typeAs specified by target_type

Syntax

  1. CAST (<expression> AS <target_type>)
  2. <target_type> ::= <domain_or_non_array_type> | <array_datatype>
  3. <domain_or_non_array_type> ::=
  4. !! See Scalar Data Types Syntax !!
  5. <array_datatype> ::=
  6. !! See Array Data Types Syntax !!

Table 8.5.1.1 CAST Function Parameters

ParameterDescription

expression

SQL expression

sql_datatype

SQL data type

CAST converts an expression to the desired data type or domain. If the conversion is not possible, an error is raised.

Casting BLOBsSuccessful casting to and from BLOBs is possible since Firebird 2.1.

8.5.1.1 Shorthand Syntax

Alternative syntax, supported only when casting a string literal to a DATE, TIME or TIMESTAMP:

  1. 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.

Note

Since Firebird 4.0 the use of 'NOW', 'YESTERDAY' and 'TOMORROW' in the shorthand cast is no longer allowed; only literals defining a fixed moment in time are supported.

8.5.1.2 Allowed Type Conversions

The following table shows the type conversions possible with CAST.

Table 8.5.1.2.1 Possible Type-castings with CAST

FromTo

Numeric types

Numeric types [VAR]CHAR BLOB

[VAR]CHAR BLOB

[VAR]CHAR BLOB Numeric types DATE TIME TIMESTAMP

DATE TIME

[VAR]CHAR BLOB TIMESTAMP

TIMESTAMP

[VAR]CHAR BLOB DATE TIME

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).

8.5.1.3 Casting Parameters

Since Firebird 2.0, you can cast statement parameters to a data type:

  1. cast (? as integer)

This gives you control over the type of the parameter set up by the engine. Please notice that with statement parameters, you always need a full-syntax cast — shorthand casts are not supported.

8.5.1.4 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 TRUE or NULL! So, given the following statements:

  1. create domain quint as int check (value >= 5000);
  2. select cast (2000 as quint) from rdb$database;
  3. select cast (8000 as quint) from rdb$database;
  4. select cast (null as quint) from rdb$database;

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:

  1. select cast (2000 as type of quint) from rdb$database;
  2. 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:

  1. create domain iso20 varchar(20) character set iso8859_1;
  2. create domain dunl20 varchar(20) character set iso8859_1 collate du_nl;
  3. create table zinnen (zin varchar(20));
  4. commit;
  5. insert into zinnen values ('Deze');
  6. insert into zinnen values ('Die');
  7. insert into zinnen values ('die');
  8. insert into zinnen values ('deze');
  9. select cast(zin as type of iso20) from zinnen order by 1;
  10. -- returns Deze -> Die -> deze -> die
  11. select cast(zin as type of dunl20) from zinnen order by 1;
  12. -- returns deze -> Deze -> die -> Die

Warning

If a domain’s definition is changed, existing CASTs to that domain or its type may become invalid. If these CASTs occur in PSQL modules, their invalidation may be detected. See the note The RDB$VALID_BLR field, in Appendix A.

8.5.1.5 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.

  1. create table ttt (
  2. s varchar(40) character set utf8 collate unicode_ci_ai
  3. );
  4. commit;
  5. select cast ('Jag har många vänner' as type of column ttt.s)
  6. from rdb$database;

Warnings

If a column’s definition is altered, existing CASTs to that column’s type may become invalid. If these CASTs occur in PSQL modules, their invalidation may be detected. See the note The RDB$VALID_BLR field, in Appendix A.

8.5.1.6 Cast Examples

A full-syntax cast:

  1. select cast ('12' || '-June-' || '1959' as date) from rdb$database

A shorthand string-to-date cast:

  1. update People set AgeCat = 'Old'
  2. where BirthDate < date '1-Jan-1943'

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:

  1. update People set AgeCat = 'Old'
  2. where BirthDate < '1-Jan-1943'

However, 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:

  1. select cast('today' as date) - 7 from rdb$database