How to CAST in Snowflake

Galaxy Glossary

How do I use CAST in Snowflake to change data types?

CAST converts a value from one data type to another in Snowflake.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What does the CAST function do in Snowflake?

CAST converts a value from its current data type to a target data type. Use it to change strings to dates, numbers to strings, or timestamps to dates, ensuring your data fits the schema you need.

How do you write CAST syntax?

Write CAST(expression AS data_type) or use the shorthand expression::data_type. Both forms behave the same; the full keyword form is clearer in shared code.

CAST vs :: shorthand—when should you use each?

Use the full CAST() form in production code for readability. The :: shorthand is handy for quick ad-hoc queries in Galaxy’s editor.

How do you cast VARCHAR order dates to DATE?

When Orders.order_date is stored as VARCHAR, convert it to DATE so Snowflake can sort and filter correctly:

SELECT id,
CAST(order_date AS DATE) AS order_date
FROM Orders
ORDER BY order_date DESC;

How do you convert numeric strings to NUMBER?

If Products.price is VARCHAR, CAST it to NUMBER before arithmetic:

SELECT id,
name,
CAST(price AS NUMBER(10,2)) AS price_num,
stock * CAST(price AS NUMBER(10,2)) AS inventory_value
FROM Products;

How do you safely CAST with possible bad data?

Use TRY_CAST to avoid runtime errors when data might not convert:

SELECT id,
TRY_CAST(email AS VARCHAR) AS safe_email
FROM Customers;

Best practices for CAST in production

1) Pick explicit precision and scale for NUMBER to prevent silent rounding. 2) Always document casts in pull requests. 3) Prefer TRY_CAST during data loading, then fix rows that return NULL.

Common mistakes and fixes

Implicit rely: Assuming Snowflake will auto-cast can break joins. Always CAST explicitly.
Wrong precision: Casting to NUMBER without scale can truncate decimals. Declare scale, e.g., NUMBER(10,2).

FAQ

Is CAST slower than ::?

No. Both compile to the same plan; performance is identical.

Does CAST change stored data?

CAST only affects query output. Use ALTER TABLE to change a column’s type permanently.

When should I use CONVERT instead of CAST?

CONVERT is an alias retained for compatibility. Prefer CAST for clarity.

Why How to CAST in Snowflake is important

How to CAST in Snowflake Example Usage


-- Convert VARCHAR date, numeric string, and join safely
SELECT o.id,
       CAST(o.order_date AS DATE)          AS order_dt,
       CAST(oi.quantity AS NUMBER(5))      AS qty,
       CAST(p.price AS NUMBER(10,2))       AS unit_price,
       qty * unit_price                    AS line_total
FROM Orders      o
JOIN OrderItems  oi ON oi.order_id = o.id
JOIN Products    p  ON p.id = oi.product_id
WHERE order_dt >= '2023-01-01';

How to CAST in Snowflake Syntax


CAST(expression AS data_type)
--or shorthand
expression::data_type

/* key parameters */
expression   Any valid Snowflake expression
data_type    Target type, e.g., DATE, NUMBER(precision, scale), VARCHAR(length)

/* ecommerce example */
SELECT id,
       CAST(order_date AS DATE) AS order_dt,
       total_amount::NUMBER(10,2) AS total_amt
FROM Orders;

Common Mistakes

Frequently Asked Questions (FAQs)

Does CAST affect table storage?

No. CAST only affects the result set. Use ALTER TABLE to change storage type.

What happens when CAST fails?

Snowflake throws a conversion error. Use TRY_CAST to return NULL instead.

Can I nest CASTs?

Yes. CAST(CAST(col AS VARCHAR) AS NUMBER) is valid but avoid for readability.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.