Available in versions: Dev (3.20) | Latest (3.19)

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

Cast expressions (with TRY_CAST)

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The standard SQL CAST expression will error when a value cannot be cast to the target data type.

Some dialects allow for ignoring such errors, returning NULL instead. This can be useful when sanitising data that is not well structured.

SELECT TRY_CAST("a", INTEGER)
create.select(tryCast("a", INTEGER)).fetch();

Dialect support

This example using jOOQ:

tryCast("a", VARCHAR(10))

Translates to the following dialect specific expressions:

BigQuery

safe_cast('a' AS string)

ClickHouse

accurateCastOrNull('a', 'Nullable(String(10))')

DuckDB, SQLDataWarehouse, SQLServer, Snowflake, Trino

try_cast('a' AS varchar(10))

Oracle

CAST('a' AS varchar2(10) DEFAULT NULL ON CONVERSION ERROR)

ASE, Access, Aurora MySQL, Aurora Postgres, CockroachDB, DB2, Derby, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB, MemSQL, MySQL, Postgres, Redshift, SQLite, Sybase, Teradata, Vertica, YugabyteDB

/* UNSUPPORTED */
Generated with jOOQ 3.20. Translate your own SQL on our website

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo