SQL TRY_CAST function
Attempts a conversion and returns NULL instead of raising an error when the value cannot convert: the safe path for user-entered and imported data.
- Kategorie
- Conversion
- Returns
the target type, or NULL- In this category
- 3 of 39
Syntax
TRY_CAST(expression AS type) -- SQL Server 2012+
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Ja | Supported under this name. |
| PostgreSQL | Not built in | no TRY_CAST; pg_input_is_valid (16+) tests first |
| MySQL | Not built in | no TRY_CAST; CAST turns bad input into 0 with a warning |
| SQLite | Different name or syntax | CAST never errors; invalid text becomes 0 or 0.0 |
| Oracle | Different name or syntax | CAST(x AS NUMBER DEFAULT NULL ON CONVERSION ERROR) and VALIDATE_CONVERSION, both 12.2+ |
Beispiele
SQL Server
SELECT TRY_CAST(text_value AS INT) AS n FROM imports;
SQL Server
-- find the rows that failed to convert SELECT * FROM imports WHERE TRY_CAST(text_value AS INT) IS NULL AND text_value IS NOT NULL;
Oracle
-- Oracle 12.2+ SELECT CAST(text_value AS NUMBER DEFAULT NULL ON CONVERSION ERROR) AS n FROM imports;
PostgreSQL
-- PostgreSQL 16+
SELECT CASE WHEN pg_input_is_valid(text_value, 'integer')
THEN text_value::integer END AS n
FROM imports;
Gut zu wissen
- TRY_CAST and TRY_CONVERT arrived in SQL Server 2012 and return NULL on failure instead of an error. Oracle 12.2 has the same effect with CAST(x AS NUMBER DEFAULT NULL ON CONVERSION ERROR).
- SQLite's CAST never fails: CAST('abc' AS INTEGER) is 0. MySQL does the same in a SELECT, with a warning. A bad value turns into a plausible number instead of a NULL you can find.
- Silent NULLs hide bad data: pair TRY_CAST with a WHERE check (WHERE TRY_CAST(x AS INT) IS NULL AND x IS NOT NULL) to find the rows that did not make it.
Questions about TRY_CAST / TRY_CONVERT
Does TRY_CAST exist in PostgreSQL or MySQL?
No. PostgreSQL's CAST raises an error; PostgreSQL 16+ can test first with pg_input_is_valid(text, 'integer'). MySQL's CAST returns 0 with a warning in a SELECT and errors in a strict-mode INSERT. Oracle 12.2+ has CAST ... DEFAULT NULL ON CONVERSION ERROR and VALIDATE_CONVERSION.
What does SQLite's CAST do with invalid text?
It converts the longest numeric prefix and ignores the rest: CAST('12abc' AS INTEGER) is 12 and CAST('abc' AS INTEGER) is 0, with no error. Check with typeof() or a GLOB pattern when importing dirty data.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Parse text into a date in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Safe cast that returns NULL instead of an error in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Safely convert strings to dates in SQL Snippet
- Safely convert strings to numbers in SQL Snippet
Cheat sheets for the engines that have it: SQL Server, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.