CAST / TRY_CAST / CONVERT
Feb 23, 2026
·
5
min read
Category: Conversion Functions
Platform Support:
✅ Snowflake | ✅ BigQuery | ✅ Databricks
Description
Type conversion functions transform data from one type to another. CAST is standard SQL for type conversion. TRY_CAST attempts conversion and returns NULL on failure instead of error. CONVERT is an alternative syntax. Essential for data type compatibility and transformations.
Syntax
CAST (Standard SQL):
TRY_CAST (Safe conversion):
CONVERT (Alternative syntax):
:: Operator (Snowflake shorthand):
Platform-Specific Notes
Snowflake:
CAST and TRY_CAST supported
:: operator as shorthand
TO_VARCHAR, TO_NUMBER, TO_DATE functions
Flexible type coercion
BigQuery:
CAST supported (primary method)
SAFE_CAST (equivalent to TRY_CAST)
No CONVERT function
Strict type checking
Databricks:
CAST and TRY_CAST supported
:: operator supported
Compatible with Spark SQL
TO_TIMESTAMP, TO_DATE functions
Example 1: CAST - Basic Type Conversions
All Platforms:
Result:
string_to_int | string_to_decimal | int_to_string | decimal_to_string | string_to_date | string_to_timestamp | one_to_true | zero_to_false |
|---|---|---|---|---|---|---|---|
12345 | 123.45 | 12345 | 123.45 | 2024-01-15 | 2024-01-15 10:30:00 | true | false |
Example 2: TRY_CAST - Safe Conversions
Snowflake/Databricks:
BigQuery:
Sample Data (orders table):
order_id | discount_code |
|---|---|
1 | 10 |
2 | 20 |
3 | PROMO |
4 | 15 |
5 | SAVE25 |
Result:
order_id | discount_code | discount_value | discount_or_zero |
|---|---|---|---|
1 | 10 | 10 | 10 |
2 | 20 | 20 | 20 |
3 | PROMO | NULL | 0 |
4 | 15 | 15 | 15 |
5 | SAVE25 | NULL | 0 |
Example 3: Platform-Specific Conversion Functions
Snowflake:
BigQuery:
Databricks:
Example 4: Data Cleaning with CAST
All Platforms:
Sample Data (messy_data table):
customer_id | price_str | date_str | active_str |
|---|---|---|---|
1 | $1,234.56 | 20240115 | TRUE |
2 | $999.99 | 20240120 | yes |
3 | $2,500.00 | 20240125 | 0 |
Result:
customer_id | clean_price | year | is_active |
|---|---|---|---|
1 | 1234.56 | 2024 | true |
2 | 999.99 | 2024 | true |
3 | 2500.00 | 2024 | false |
Example 5: JSON to Structured Data
Snowflake:
BigQuery:
Example 6: Implicit vs Explicit Casting
All Platforms:
Common Data Type Conversions
From Type | To Type | Snowflake | BigQuery | Databricks |
|---|---|---|---|---|
STRING | INTEGER | ::INTEGER | INT64 | ::INT |
STRING | DECIMAL | ::DECIMAL(10,2) | NUMERIC | ::DECIMAL(10,2) |
STRING | DATE | ::DATE | DATE | ::DATE |
STRING | TIMESTAMP | ::TIMESTAMP | TIMESTAMP | ::TIMESTAMP |
INTEGER | STRING | ::VARCHAR | STRING | ::STRING |
DATE | STRING | ::VARCHAR | STRING | ::STRING |
TIMESTAMP | DATE | ::DATE | DATE | ::DATE |
JSON | STRUCT | PARSE_JSON | JSON | FROM_JSON |
Error Handling
CAST (throws error on failure):
TRY_CAST/SAFE_CAST (returns NULL on failure):
Performance Considerations
Explicit is better: Clear type conversions improve query optimization
Index impact: Casting indexed columns prevents index usage
Bulk operations: TRY_CAST safer for large datasets with unknown data quality
Format specifications: Platform-specific functions (TO_DATE) can be faster
Avoid casting in WHERE clauses:
Common Use Cases
Data import: Convert string data to appropriate types
API integration: Transform JSON to structured data
Data cleaning: Handle messy/inconsistent data
Type compatibility: Join tables with different column types
Display formatting: Convert for presentation
Calculations: Ensure numeric types for math operations
Best Practices
Use TRY_CAST for uncertain data: Prevents errors in production
Be explicit: Don't rely on implicit conversions
Validate first: Check data quality before casting
Handle NULLs: Use COALESCE with TRY_CAST
Choose appropriate precision: DECIMAL(10,2) vs DECIMAL(18,6)
Document conversions: Explain why casting is needed
Test edge cases: NULL, empty string, invalid formats
Use platform-specific functions: Often more feature-rich





