The direct answer: use the TO_NUMBER function (alias TO_DECIMAL, TO_NUMERIC) or the CAST expression to convert a string or variant value to a number in Snowflake. Example: SELECT TO_NUMBER('123.45') AS num; returns 123.45.
Why You Need to Convert to Number
In Snowflake, data often arrives as text—from CSV files, API payloads, or manual imports—and you need numeric values for arithmetic, aggregations, or joins. Snowflake is strict about data types: you cannot add a string like ’10’ to a number without an explicit conversion. Converting to a number also improves query performance because numeric comparisons are faster than string comparisons, and it ensures correct sorting (e.g., 10 comes after 9, not after 1).
Using TO_NUMBER: Syntax and Examples
The TO_NUMBER function converts an expression to a NUMBER data type. Its full syntax is:
TO_NUMBER(expr [, format] [, precision [, scale]])
- expr: the value to convert, typically a string or variant.
- format: optional, a string specifying the expected format (e.g., ‘999.99’).
- precision and scale: optional, define the total digits and digits after the decimal point.
Basic examples:
SELECT TO_NUMBER('123') AS a; -- 123SELECT TO_NUMBER('1,234.56') AS b; -- error: comma not recognized without format
To handle commas, specify a format:
SELECT TO_NUMBER('1,234.56', '9,999.99') AS c; -- 1234.56
If you need a specific scale, add it:
SELECT TO_NUMBER('3.14159', 10, 2) AS d; -- 3.14 (rounded)
Note: if the input string contains non-numeric characters (like ’12abc’), the function returns an error unless you use a format that skips them, or you pre-clean the data.
Using CAST and the :: Operator
Snowflake also supports the standard CAST expression and the shorthand :: operator. Both behave like TO_NUMBER but with less flexibility for formats.
SELECT CAST('42' AS NUMBER) AS e; -- 42SELECT '42'::NUMBER AS f; -- 42
For a variant column that holds a number as a string, use:
SELECT variant_col::NUMBER AS g FROM table;
If the variant contains a JSON number (not a string), the cast works directly. If it’s a string, the cast attempts to parse it. For example, PARSE_JSON('{"val":"7.5"}'):val::NUMBER returns 7.5.
Handling Errors and Non-Numeric Data
Conversion fails with a Numeric value 'abc' is not recognized error. To avoid this, use the TRY_TO_NUMBER function, which returns NULL instead of an error for invalid input.
SELECT TRY_TO_NUMBER('abc') AS h; -- NULL
This is useful when cleaning data. You can combine it with COALESCE to provide a default:
SELECT COALESCE(TRY_TO_NUMBER(col), 0) AS i FROM table;
For large datasets, consider using a CASE statement to filter out bad rows before conversion, but TRY_TO_NUMBER is simpler and faster.
Formatting and Locale Considerations
Snowflake uses the format model from Oracle, where ‘9’ represents a digit and ‘0’ forces a digit. Common formats:
- ‘999.99’ – up to 3 digits before decimal, 2 after.
- ‘9,999.99’ – includes comma grouping.
- ‘$999.00’ – dollar sign and fixed decimals (but the result is still a number, not a string).
Be careful with locale: Snowflake’s default format does not accept European decimal commas (e.g., ‘1.234,56’). You must specify a format like ‘9G999D99’ where G is the group separator and D is the decimal separator, but those are locale-dependent. Simpler: replace commas with periods before conversion, or use REPLACE:
SELECT TO_NUMBER(REPLACE('1.234,56', ',', '.')) AS j; -- 1234.56 (but note the period is also a group separator in some locales—this is a common pitfall).
Converting Between Number Types
Snowflake has several numeric types: NUMBER (default), DECIMAL, NUMERIC, INT, INTEGER, BIGINT, SMALLINT, TINYINT, BYTEINT, FLOAT, FLOAT4, FLOAT8, DOUBLE, REAL. Conversions between them are usually automatic, but you can use CAST to force a type, e.g., CAST(price AS FLOAT) for floating-point arithmetic. When converting from a float to an integer, use ROUND or TRUNC first to avoid unexpected rounding behavior.
SELECT CAST(3.99 AS INT) AS k; -- 3 (truncates, not rounds)
For precise decimal arithmetic, stick with NUMBER and specify scale.
Common Mistakes and Pitfalls
- Trailing spaces: ‘ 123 ‘ converts fine, but ‘123 ‘ with a format may fail—use
TRIMfirst. - Null handling:
TO_NUMBER(NULL)returns NULL, but if you expect 0, useCOALESCE. - Overflow: If the number exceeds the precision, Snowflake returns an error. Use a larger precision or
TRY_TO_NUMBER. - Scientific notation: Strings like ‘1e3’ are not converted by default. Use
TO_DOUBLEorCAST AS FLOAT. - Variant vs. string: A variant that contains a JSON number (e.g., 123) converts directly, but a variant containing a JSON string (‘123’) needs an explicit cast.
Frequently Asked Questions
What is the difference between TO_NUMBER and CAST?
Both convert to a number, but TO_NUMBER allows an optional format string and precision/scale arguments, while CAST only accepts a data type. For simple conversions, either works; for formatted input like ‘1,234.56’, use TO_NUMBER with a format.
How do I convert a string with commas and dollar signs?
Use TO_NUMBER with a format that includes the characters, e.g., TO_NUMBER('$1,234.56', '$9,999.99'). If the format is unknown, strip non-numeric characters with REGEXP_REPLACE first: TO_NUMBER(REGEXP_REPLACE('$1,234.56', '[^0-9.]', '')).
Why does TO_NUMBER return an error for ‘abc’?
Because ‘abc’ is not a valid numeric string. Use TRY_TO_NUMBER to get NULL instead of an error, or clean the data with REGEXP_REPLACE to remove non-numeric characters.
Can I convert a date or timestamp to a number?
Yes, but you need to decide what number you want. For Unix epoch seconds, use CAST(column AS NUMBER) on a timestamp? Actually, DATE_PART(epoch_second, timestamp_col) gives a number. For YYYYMMDD, use TO_NUMBER(TO_CHAR(date_col, 'YYYYMMDD')).
Does Snowflake automatically convert strings to numbers in comparisons?
No. If you compare a string column to a number, Snowflake may implicitly cast the string to a number, but this can cause errors if the string is not numeric. It’s better to explicitly convert.
What is the maximum precision for NUMBER?
Snowflake supports precision up to 38 digits. If you need more, consider storing as a string or using FLOAT (but FLOAT has limited precision for large numbers).
Related
- Fed Bank Balance Sheet: How It Works and Why It Matters
- String to Number Power Automate: Convert Text to Int
- Power Automate split expression: how to use it
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Not Equal Sign: How to Use the <> OperatorExcel’s not equal sign is <>. Put it between two values in a formula to check whether they…
- Unable to Share Calendar in Outlook? Fixes That WorkIf you’re unable to share a calendar in Outlook, first check that it belongs to an Exchange or…
- Remove Section Breaks in Word Without Losing FormattingTo remove a section break in Microsoft Word, show formatting marks with Home > ¶, select the section…
- Convert Text to Columns Wizard in Excel: How It WorksIn Excel for Microsoft 365 on Windows, select the cells containing the text, then choose Data > Text…
- How to Move Tabs in Excel With the KeyboardTo move a worksheet tab in Excel for Microsoft 365 on Windows without using the mouse, press Alt,…
- Excel Template for Quotation Comparison: Build OneIn Excel for Microsoft 365 on Windows, create supplier columns in B:D, enter each quote’s comparable costs in…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.