Snowflake String to Number: CAST and TO_NUMBER Explained

How-to basics · Updated November 23, 2026

To convert a string to a number in Snowflake, use the CAST function (e.g., CAST('123.45' AS NUMBER(10,2))) or the shorthand :: operator (e.g., '123.45'::NUMBER(10,2)). For more control over formatting, use TO_NUMBER (or TO_DECIMAL), which accepts optional format and precision parameters.

Why Convert Strings to Numbers in Snowflake?

Snowflake stores data in columns with explicit types, but data loaded from CSV, JSON, or external APIs often arrives as strings. Even when a column looks numeric, Snowflake may treat it as VARCHAR if the source file declared it so, or if the values contain leading zeros, currency symbols, or thousand separators. Converting strings to numbers is essential for arithmetic operations, aggregations (SUM, AVG), sorting numerically, and joining on numeric keys. Without conversion, Snowflake will either error out or return lexicographic (alphabetical) order, which can misplace values like 100 before 20.

Core Conversion Functions

Snowflake offers three primary ways to convert a string to a number:

  • CAST(string AS NUMBER) – Standard SQL syntax. Example: CAST('42' AS NUMBER) returns 42.
  • string::NUMBER – Shorthand for CAST. Example: '3.14'::NUMBER(10,2) returns 3.14.
  • TO_NUMBER(string, format, precision, scale) – Flexible function with optional format model. Example: TO_NUMBER('$1,234.56', '$9,999.99') returns 1234.56.

All three return a numeric data type (NUMBER, DECIMAL, FLOAT, etc.) depending on the target type you specify. The default NUMBER precision is 38, scale 0, which rounds to an integer if you don’t specify scale.

How to Choose the Right Function

Use CAST or :: when your strings are simple numbers without formatting (e.g., ‘123’, ‘45.67’). These are the most readable and portable. Use TO_NUMBER when your strings include currency symbols, commas, parentheses for negatives, or other locale-specific formatting. For example, to convert ‘($1,000.50)’ (a common accounting negative format), you would use TO_NUMBER('($1,000.50)', '$9,999.99') which returns -1000.50. If you need to handle European decimal commas (e.g., ‘1.234,56’), you can specify a format like '9G999D99' or use REPLACE first. For production, prefer explicit CAST for clarity, but know that TO_NUMBER is more powerful for messy data.

Step-by-Step Examples with Real Data

Assume a table sales_raw with a VARCHAR column amount_str containing values like ‘1,234.56’, ‘0.99’, and ‘ -45 ‘. Here’s how to convert them:

  1. Simple conversion: SELECT amount_str::NUMBER(10,2) AS amount FROM sales_raw; – This fails on ‘1,234.56’ because of the comma.
  2. Using TO_NUMBER with format: SELECT TO_NUMBER(amount_str, '9,999.99') AS amount FROM sales_raw; – This handles the comma and spaces (Snowflake trims spaces by default).
  3. If you have mixed formats, use a CASE or TRY_TO_NUMBER: SELECT TRY_TO_NUMBER(amount_str) AS amount FROM sales_raw; – TRY_TO_NUMBER returns NULL for strings that don’t convert, rather than erroring.

For a full table update, you might do: UPDATE sales_raw SET amount_num = TRY_TO_NUMBER(amount_str); after adding a numeric column.

Common Pitfalls and How to Avoid Them

  • Precision/Scale truncation: If you cast ‘123.456’ to NUMBER(5,2), Snowflake rounds to 123.46. Be explicit about the scale you need.
  • Leading/trailing spaces: CAST does not trim spaces; ‘ 42’ will fail. Use TRIM or TRY_TO_NUMBER which handles spaces.
  • Non-numeric characters: CAST(‘$100’ AS NUMBER) fails. Use TO_NUMBER with a format, or strip characters with REPLACE first.
  • Scientific notation: Strings like ‘1E3’ are not converted by CAST unless you use TO_DOUBLE. Use TO_DOUBLE('1E3') to get 1000.0.
  • NULL handling: CAST(NULL AS NUMBER) returns NULL. If you want a default, use COALESCE after conversion.

Comparing CAST, TO_NUMBER, and TRY_TO_NUMBER

The table below summarizes the differences:

  • CAST / :: – Strict, errors on any invalid character. Best for clean data.
  • TO_NUMBER – Accepts a format string, allows custom symbols. Errors on invalid input if format doesn’t match.
  • TRY_TO_NUMBER – Same as TO_NUMBER but returns NULL instead of erroring. Ideal for ETL pipelines where you want to flag bad rows later.

Performance-wise, all are similar; the main cost is parsing. If you have millions of rows, consider using TRY_TO_NUMBER to avoid query failures.

Handling Special Formats: Currency, Percent, and Dates

Currency: Use format elements like '$9,999.99' for dollar amounts, or 'L9,999.99' for locale currency symbol. Percent: To convert ‘12.5%’ to 0.125, first strip ‘%’ with REPLACE then divide by 100: REPLACE('12.5%', '%')::NUMBER(10,2) / 100. Dates: If you have a date string like ‘2023-01-15’ and you want a number like 20230115, use TO_NUMBER(REPLACE('2023-01-15', '-', '')). For timestamps, you might want to extract epoch seconds with DATE_PART instead of converting the string directly.

Frequently Asked Questions

What is the difference between TO_NUMBER and TO_DECIMAL?

They are aliases in Snowflake. Both perform the same conversion; TO_DECIMAL is provided for compatibility with other databases. Use whichever reads better in your code.

How do I convert a string with commas to a number?

Use TO_NUMBER('1,234.56', '9,999.99') or first remove commas with REPLACE('1,234.56', ',', '')::NUMBER. The format approach is cleaner and handles multiple commas automatically.

Why does CAST(‘123.45’ AS INT) fail?

Snowflake’s INT is an alias for NUMBER(38,0), which has scale 0. Casting a decimal string to scale 0 causes an error because Snowflake does not implicitly round in CAST. Use CAST('123.45' AS NUMBER(10,2)) or ROUND first.

Can I convert a string to a float in Snowflake?

Yes, use CAST('3.14' AS FLOAT) or '3.14'::FLOAT. For scientific notation, use TO_DOUBLE('1.5e3') which returns 1500.0.

What happens if I try to convert a NULL or empty string?

CAST(NULL AS NUMBER) returns NULL. CAST(” AS NUMBER) returns an error because an empty string is not a valid number. To treat empty as NULL, use NULLIF('', '')::NUMBER or TRY_TO_NUMBER which returns NULL for empty strings.

Is there a way to convert strings to numbers in a SELECT without changing the table?

Yes, just use the conversion in your SELECT clause, e.g., SELECT amount_str::NUMBER(10,2) AS amount FROM table. The result set contains numbers, but the underlying table remains unchanged.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.