dataengref migrate

Home / SQL Server

SQL Server TRY_CAST in Snowflake, Databricks and BigQuery

Snowflake and Databricks both have TRY_CAST; BigQuery calls it SAFE_CAST. Snowflake only accepts string input, so TRY_CAST on a number or date column fails there.

SQL Server function: TRY_CAST(expression AS data_type [(length)]) · SQL Server docs

Equivalents

TargetSupportSyntaxNotesSource
SnowflakeSame nameTRY_CAST(source_string_expr AS target_data_type)Only string expressions are accepted as input.Snowflake docs
DatabricksSame nametry_cast(sourceExpr AS targetType)Databricks SQL and Databricks Runtime 10.4 LTS and above.Databricks docs
BigQueryDifferent nameSAFE_CAST(expr AS type)Returns NULL instead of an error.BigQuery docs

Behavior differences to check

Snowflake

Examples

Parse amounts stored as text

SQL Server
SELECT TRY_CAST(amount_text AS DECIMAL(12, 2)) AS amount FROM staging;
Snowflake
SELECT TRY_CAST(amount_text AS DECIMAL(12, 2)) AS amount FROM staging;
Databricks
SELECT try_cast(amount_text AS DECIMAL(12, 2)) AS amount FROM staging;
BigQuery
SELECT SAFE_CAST(amount_text AS NUMERIC) AS amount FROM staging;

Other SQL Server functions

Last checked: · Paste a whole query into the SQL checker to find every function that needs a change.