CAST / CONVERT / TRY_CAST
Feb 23, 2026
·
5
min read
Category: Conversion Functions
Platform Support:
✅ Snowflake | ✅ BigQuery | ✅ Databricks
Description
Convert data from one type to another. CAST is standard SQL, CONVERT is platform-specific, and TRY_CAST returns NULL on conversion failure instead of error. Essential for data type transformations, cleaning, and ensuring type compatibility.
Syntax
CAST (Standard SQL):
TRY_CAST (Safe Conversion):
CONVERT:
Platform-Specific Notes
Snowflake:
CAST and :: operator
TRY_CAST for safe conversion
TO_* functions (TO_NUMBER, TO_DATE, etc.)
Extensive format support
BigQuery:
CAST standard
SAFE_CAST instead of TRY_CAST
Format strings in PARSE_DATE, PARSE_TIMESTAMP
Strict type checking
Databricks:
CAST and :: operator
TRY_CAST supported
Standard Spark SQL conversion functions
Compatible with Hive types
Example 1: Basic Type Conversions
All Platforms:
Result:
str_to_int | str_to_decimal | int_to_str | decimal_to_str | str_to_date | date_to_str | str_to_timestamp |
|---|---|---|---|---|---|---|
123 | 123.45 | "123" | "123.45" | 2024-01-15 | "2024-01-20" | 2024-01-15 10:30:00 |
Example 2: TRY_CAST - Safe Conversion
Snowflake/Databricks:
BigQuery:
Sample Data:
value |
|---|
123 |
456 |
invalid |
NULL |
Result:
value | cast_result | try_cast_result |
|---|---|---|
123 | 123 | 123 |
456 | 456 | 456 |
invalid | ERROR | NULL |
NULL | NULL | NULL |
Example 3: Data Cleaning with TRY_CAST
All Platforms:
Sample Data (customer_import table):
customer_id | phone_raw | email_raw | age_raw |
|---|---|---|---|
1 | 555-1234 | 35 | |
2 | 555-5678 | forty | |
3 | invalid | 28 | |
4 | 555-9012 | NULL |
Result (filtered for valid ages):
customer_id | phone_raw | age_raw | age_clean | data_quality_flag |
|---|---|---|---|---|
1 | 555-1234 | 35 | 35 | Valid |
3 | invalid | 28 | 28 | Valid |
4 | 555-9012 | NULL | NULL | Valid |
Example 4: Date/Time Conversions
Snowflake:
BigQuery:
Databricks:
Example 5: Numeric Precision Control
All Platforms:
Result:
price | as_integer | as_decimal_0 | as_decimal_2 | as_decimal_4 | rounded_2 |
|---|---|---|---|---|---|
123.456789 | 123 | 123 | 123.46 | 123.4568 | 123.46 |
Example 6: JSON to Typed Data
Snowflake:
BigQuery:
Databricks:
Common Data Type Conversions
From Type | To Type | Example |
|---|---|---|
STRING | INTEGER |
|
STRING | DECIMAL |
|
STRING | DATE |
|
STRING | TIMESTAMP |
|
STRING | BOOLEAN |
|
INTEGER | STRING |
|
DECIMAL | INTEGER |
|
DATE | STRING |
|
TIMESTAMP | DATE |
|
BOOLEAN | STRING |
|
CAST vs TRY_CAST Decision
Scenario | Use | Reason |
|---|---|---|
Trusted data | CAST | Fail fast on errors |
User input | TRY_CAST | Handle invalid gracefully |
Data quality checks | TRY_CAST | Identify bad data |
ETL pipeline | TRY_CAST | Prevent pipeline failures |
Known format | CAST | Performance (no error handling) |
Mixed quality | TRY_CAST | Filter out bad records |
Common Conversion Patterns
Clean and filter invalid data:
Provide defaults for failed conversions:
Track conversion failures:
Performance Considerations
CAST is faster than TRY_CAST (no error handling)
Implicit conversions can slow queries
Convert once, store properly typed
Index converted columns if filtered frequently
Use appropriate precision to save storage
Best Practices
Use TRY_CAST for untrusted data: Prevent query failures
Specify precision: Don't rely on defaults
Validate before converting: Check formats first
Handle NULLs explicitly: Use COALESCE if needed
Document format assumptions: Especially for dates
Test edge cases: Empty strings, special characters
Use appropriate types: Don't store numbers as strings
Consider performance: CAST in WHERE can prevent index use





