Available in versions: Dev (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)

-- DUCKDB, SNOWFLAKE, SQLDATAWAREHOUSE, SQLSERVER, TRINO
try_cast('a' AS varchar(10))

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

-- ACCESS, ASE, AURORA_MYSQL, AURORA_POSTGRES, COCKROACHDB, DB2, DERBY, EXASOL, FIREBIRD, H2, HANA, HSQLDB, INFORMIX, 
-- MARIADB, MEMSQL, MYSQL, POSTGRES, REDSHIFT, SQLITE, SYBASE, TERADATA, VERTICA, YUGABYTEDB
/* UNSUPPORTED */

(These are currently generated with jOOQ 3.19, see #10141), or translate your own on our website

Feedback

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

The jOOQ Logo