Conversion functions¶
This family of functions can be used to convert an expression of any Snowflake data type to another data type.
List of functions¶
Sub-category |
Function |
Notes |
---|---|---|
Any data type |
||
Error-handling version of CAST. |
||
Text/character/binary data types |
||
Error-handling version to TO_BINARY. |
||
Numeric data types |
||
Error-handling versions of TO_DECIMAL, TO_NUMBER, etc. |
||
Error-handling version of TO_DOUBLE. |
||
Boolean data type |
||
Error-handling version of TO_BOOLEAN. |
||
Date and time data types |
||
Error-handling version of TO_DATE. |
||
Error-handling version of TO_TIME. |
||
Error-handling versions of TO_TIMESTAMP, etc. |
||
Semi-structured data types |
||
Geospatial data types |
||
Error-handling version of TO_GEOGRAPHY |
||
Error-handling version of TO_GEOMETRY |
||
Error-handling conversion functions¶
Conversion functions with a TRY_ prefix are special versions of their respective conversion functions. These functions return a NULL value instead of raising an error when the conversion cannot be performed:
These functions only support string expressions (i.e. VARCHAR or CHAR data type) as input.
Important
These error-handling conversion functions are optimized for situations where conversion errors are relatively infrequent:
If there are no (or very few) errors, they should result in no visible performance impact.
If there are a large number of conversion failures, using these functions can result in significantly slower performance. Also, when using them with the VARIANT type, some operations might result in reduced performance.
Numeric formats in conversion functions¶
The functions TO_DECIMAL , TO_NUMBER , TO_NUMERIC, and TO_DOUBLE accept an optional parameter that specifies the format of the input string, if the input expression evaluates to a string. For more information about the values this parameter can have, see SQL format models.
Date and time formats in conversion functions¶
The following functions allow you to specify the expected date, time, or timestamp format to parse or produce a string:
You specify the format in an optional argument, using the following case-insensitive elements to describe the format:
Format element |
Description |
---|---|
|
Four-digit year. |
|
Two-digit year, controlled by the TWO_DIGIT_CENTURY_START session parameter. For example, when set to |
|
Two-digit month ( |
|
Full or abbreviated month name. |
|
Full month name. |
|
Two-digit day of month ( |
|
Abbreviated day of week. |
|
Two digits for hour ( |
|
Two digits for hour ( |
|
Ante meridiem ( |
|
Two digits for minute ( |
|
Two digits for second ( |
|
Fractional seconds with precision |
|
Time zone hour and minute, offset from UTC. Can be prefixed by |
|
Four-digit year in ISO format, which are negative for BCE years. |
Note
When a date-only format is used, the associated time is assumed to be midnight on that day.
Anything in the format between double quotes or other than the above elements is parsed/formatted without being interpreted.
For more details about valid ranges, number of digits, and best practices, see Additional information about using date, time, and timestamp formats.
Usage notes¶
Anything in the format between double quotes or other than the above elements is parsed/formatted without being interpreted.
Examples¶
Convert a string to a date using a specified input format of dd/mm/yyyy
. The display format for dates in the output
is determined by the DATE_OUTPUT_FORMAT session parameter (default YYYY-MM-DD
).
SELECT TO_DATE('3/4/2024', 'dd/mm/yyyy');
+-----------------------------------+
| TO_DATE('3/4/2024', 'DD/MM/YYYY') |
|-----------------------------------|
| 2024-04-03 |
+-----------------------------------+
Convert a date to a string, and specify a date output format
of mon dd, yyyy
.
SELECT TO_VARCHAR('2024-04-05'::DATE, 'mon dd, yyyy');
+------------------------------------------------+
| TO_VARCHAR('2024-04-05'::DATE, 'MON DD, YYYY') |
|------------------------------------------------|
| Apr 05, 2024 |
+------------------------------------------------+
Binary formats in conversion functions¶
TO_CHAR , TO_VARCHAR, and TO_BINARY accept an optional argument specifying the expected format to parse or produce a string.
The format can be one of the following strings (case-insensitive):
HEX
BASE64
UTF-8
For more information about these formats, see Overview of supported binary formats.
For examples of using these formats, see the Examples section of Binary input and output.