Date and time are important data types for analyzing trends and patterns during data analysis. They also help manage deadlines and inventories by comparing changes in resources over a period of time. To perform effective data analytics, you should consider storing your data in a warehouse that supports date and time data types.

Snowflake is a platform that supports various data types and offers numerous data management functions. You can use Snowflake CAST as date data conversion function and change any data type to date format. TO_DATE is another Snowflake conversion function that enables you to specifically convert a string data type to a date format for reliable data integration and analytics. 

This blog offers a well-defined guide on how to use the Snowflake TO_DATE function judiciously in various scenarios to simplify the management of date and time data types. 

Understanding the TO_DATE Function in Snowflake

The TO_DATE function in Snowflake converts a date in string format to a date data type. You can use this conversion function to analyze date-related information in your Snowflake SQL queries. The TO_DATE is a reliable function as it allows you to work with various date formats, provides numerous customization options, and handles errors efficiently. 

The syntax for the TO_DATE function is as follows:

TO_DATE( <string_expr> [, <format> ] )

TO_DATE( <timestamp_expr> )

TO_DATE( '<integer>' )

TO_DATE( <variant_expr> )

OR

TO_DATE(string_expression[, format_mask][, language])

The syntax consists of the following arguments:

  • The string_expression is the date string you desire to convert to the date data type. It can be a column name or any other string expression. 
  • The timestamp_expression evaluates the data portion of the timestamp data type.
  • The integer expression evaluates the integer data in the form of a string. 
  • The variant_expr should contain data in the form of string, date, or string containing an integer number of seconds or milliseconds. 
  • The format_mask is an optional parameter that specifies the format of the input string. 
  • The language is also an optional parameter and indicates the language of the input string. 

Here’s a simple example of the TO_DATE function:

SELECT TO_DATE('2024-05-10'), DATE('2024-05-10');

The output is as follows:

TO_DATE(‘2024-05-10’)DATE(‘2024-05-10’)
2024-05-102024-05-10
Snowflake TO_DATE: Example of TO_DATE Function

The data type of output returned after deploying the TO_DATE function is a date. 

The TO_DATE is a date conversion function for data types in Snowflake. Other than this, there are many other date and time functions in Snowflake, which are comprehensively listed below:

Sub-categoryFunctionNotes
ConstructionDATE _FROM_PARTS
TIME_FROM_PARTS
TIMESTAMP_FROM_PARTS
ExtractionDATA_PART
DAYNAME
HOUR/MINUTE/SECONDAlternative for DATA_PART.
LAST_DAYAccepts relevant date parts (see next section for details)
MONTHNAME
NEXT_DAY
PREVIOUS_DAY
YEAR*/DAY*/WEEK*/MONTH/QUARTERAlternative for DATE_PART
EXTRACTAlternative for DATA_PART.
Snowflake TO_DATE: Date and Time Functions in Snowflake
Addition/SubtractionADD_MONTHS
DATEADDAccepts relevant date parts (see next section for details).
DATEDIFAccepts relevant date parts (see next section for details).
MONTHS_BETWEEN
TIMEADDAlias for DATEADD.
TIMEIFFAlias for DATEDIFF.
TIMESTAMPADDAlias for DATEADD.
TIMESTAMPDIFFAlias for DATEDIFF.
TruncationDATE_TRUNCAccepts relevant date parts (see next section for details).
TIME_SLICEAccepts relevant date parts (see next section for details).
TRUNCAlternative for DATE_TRUNC.
ConversionTO_DATE , DATE
TO_TIME , TIME
TO_TIMESTAMP / TO_TIMESTAP_*
Time ZoneCONVERT_TIMEZONE
AlertsLAST_SUCCESSFUL_SCHEDULED_TIME
SCHDULED TIME
Snowflake TO_DATE: Date and Time Functions in Snowflake

Working with Different Date Formats in Snowflake

You can query different date formats in Snowflake. Some of these date formats are:

  • YYYY-MM-DD: Standard date format 
  • DD/MM/YYYY: European date format 
  • DD-MMM-YYYY: Day abbreviation month year 
  • MM/DD/YYYY: U.S. date format 
  • HH24:MI:SS: 24-hour time format 

In addition to supporting standard date formats, Snowflake allows you to use customized date formats according to your requirements. The format_mask parameter enables you to specify the date format of your choice while using TO_DATE in Snowflake. With these capabilities, Snowflake enables you to work efficiently with any date format in your organization. 

As a best practice, you should always explicitly specify the date format you want in the format_mask parameter of the query. This is helpful when you use formats like ’03-04-2016,’ which could be interpreted as either April 3 or March 4, depending on the region.

Use Cases of Snowflake TO_DATE Function With Examples

Some common date and time queries using the Snowflake TO_DATE function are as follows:

  • Basic Date Conversion

The example below shows how to insert a value in the DATE1 column:

CREATE TABLE my_table(id INTEGER, date1 DATE);
--July 23, 2016.
INSERT INTO my_table(id, date1) VALUES (1, TO_DATE('2016.07.23','YYYY.MM.DD'));
--NULL.
INSERT INFO my_table(id) VALUES (2);
SELECT id, date1
    FROM my_table
    ORDER BY id;
+-------+------------+
|  ID   |  DATE1     |
+-------+------------+
|   1   | 2016-07-23 |
|   2   |    NULL    |
+-------+------------+
  • Date Conversion to a Specific Format

You can change the format of the date while using the TO_DATE function as follows:

SELECT TO _VARCHAR(TO_DATE('12/09/2023' , 'DD/MM/YYYY'), 'MM-DD-YYYY');

Here, you can see that the date format is converted from DD/MM/YYYY to MM-DD-YYYY format. 

  • Extracting Date from Timestamp

You can use the combination of Snowflake timestamp TO_DATE as shown in the below example. The TO_DATE function accepts values and strings in TIMESTAMP format but eliminates the time information in the output. 

INSERT INTO my_table(id, date1) VALUES
    (3, TO_DATE('2020.02.20 11:15:00', 'YYYY.MM.DD HH:MI:SS')),
    (3, TO_TIMESTAMP('2020.02.20 04:00:00', 'YYYY.MM.DD HH:MI:SS')),
+-------+------------+
|  ID   |  DATE1     |
+-------+------------+
|   3   | 2020-02-20 |
|   4   | 2020-02-24 |
+-------+------------+

If you insert a date with only time values, the output date will be January 1, 1970, as it is the default date

INSERT INTO my_table(id, date1) VALUES
(5, TO_DATE('11:20:30' , 'hh:mi:ss'));
SELECT id, date1 
FROM my_table
WHERE id = 5;
+-------+------------+
|  ID   |  DATE1     |
+-------+------------+
|   5   | 1970-01-01 |
+-------+------------+
  • Using TO_DATE While Data Loading

You can change the string information to date data type while loading data to Snowflake using the TO_DATE function. 

SELECT TO_DATE(start_time) AS date,
pipe_name,
SUM (credits_used) AS credits_used
FROM snowflake. account_usage. pipe_usage_history
WHERE start_time >= DATEADD (month, -1, CURRENT_TIMESTAMP())
GROUP BY 1,2
ORDER BY 3 DESC;

In this query, the Snowflake TO_DATE function converts the ‘start_time’ attribute from string to date data type while loading the data. 

  • Converting and Filtering Data

You can use the Snowflake TO_DATE function along with the WHERE clause in SQL to convert and filter your data while converting it to date format. It is used to filter date data types with specific conditions in large datasets. 

For instance, consider the example below to determine the daywise consumption of pipes and credits in the last 30 days. The condition restricts the retrieved data from including records where the start_time is within the last 30 days. It also conveys that the queried data should include rows where the start_time date is greater than or equal to the date that was one month before the current time. For example, if the current date is 25 May, the retrieved data will include all dates since 25 April. 

SELECT TO_DATE(start_time) AS date,
pipe_name,
SUM (credits_used) AS credits_used
FROM snowflake. account_usage. pipe_usage_history
WHERE start_time >= DATEADD (month, -1, CURRENT_TIMESTAMP())
GROUP BY 1,2
ORDER BY 3 DESC;

Applying Date Arithmetic

You can use the Snowflake TO_DATE function along with other date functions of Snowflake to perform arithmetic tasks like addition or subtraction on date time data type. Consider the example below:

SELECT DATEDIFF('day', TO_DATE ('12-jan-2016 00:00:00','dd-mon-yyyy hh:mi:ss'), current_date() );

+----------------------------------------------------------------------------------+

| DATEDIFF('DAY', TO_DATE ('12-JAN-2016 00:00:00','DD-MON-YYYY HH:MI:SS'), CURRENT_DATE() ) |

|---------------------------------------------------------------------------------|

|                                     240                                         |

+----------------------------------------------------------------------------------+

Error Handling While Using Snowflake TO_DATE Function

Despite the versatility of the TO_DATE function, errors can occur while deploying it in queries. For example, there might be inconsistencies in the format mask parameter, or the format type can be invalid, resulting in an error in conversion. You can use the following approach to resolve such issues:

  • Use TRY_TO_DATE

The Snowflake TRY_TO_DATE function returns a NULL value rather than raising an error if the conversion cannot be performed. Consider an example below:

SELECT 
  TRY_TO_DATE('2024-05-10') AS valid_date,
  TRY_TO_DATE('Invalid') AS valid_date,
+-----------+-------------+
| VALID_DATE| INVALID_DATE|
|-----------+-------------|
| 2024-05-10| NULL        | 
+-----------+-------------+
  • Use COALESCE function with TO_DATE

The COALESCE function in Snowflake returns the first non-NULL values when conversion fails. If all the values are NULL, it returns NULL. You can use it with TO_DATE to get the first non-NULL values instead of an error as output. 

SELECT column1, column2, column3, COALESCE(column1, column2, column3)
FROM (
    VALUES
    (1, 2, 3),
    (3, NULL, 2),
    (NULL, NULL, 3),
    (NULL, NULL, NULL),
    (1, NULL, 3),
    (1, NULL, NULL),
    (2, NULL, NULL),
    (1, NULL, NULL)
) v;

+---------+---------+---------+-------------------------------------+
| COLUMN1 | COLUMN2 | COLUMN3 | COALESCE(COLUMN1, COLUMN2, COLUMN3) |
|---------+---------+---------+-------------------------------------|
|    1    |     2   |      3  |                                1    |
|  NULL   |     2   |      3  |                                2    |
|  NULL   |   NULL  |      3  |                                3    |
|  NULL   |   NULL  |   NULL  |                              NULL   |
|     1   |   NULL  |      3  |                                1    |
|     1   |   NULL  |   NULL  |                                1    | 
|     1   |     2   |   NULL  |                                1    |
+---------+---------+---------+-------------------------------------+

Use of Hevo Data in Deploying Snowflake TO_DATE Function

You can avoid inconsistencies in the data format during data integration with the help of the TO_DATE Snowflake function. Data integration involves data consolidation from multiple sources, along with pre-processing and transformation.

While loading data to Snowflake, you can use the TO_DATE function to transform the string data into your preferred date format. To ease the data integration process, you can use automated tools like Hevo Data that provide you with a seamless data integration experience. 

Hevo Data is a no-code ELT platform that provides real-time data integration and a cost-effective way to automate your data pipeline workflow. With over 150 source connectors, you can integrate your data into multiple platforms, conduct advanced analysis on your data, and produce useful insights.

Here are some of the most important features provided by Hevo Data:

  • Data Transformation: Hevo Data provides you the ability to transform your data for analysis with a simple Python-based drag-and-drop data transformation technique. It can help to eliminate any inconsistencies in the date format before it is integrated into the Snowflake warehouse. 
  • Automated Schema Mapping: Hevo Data automatically arranges the destination schema to match the incoming data. It also lets you choose between Full and Incremental Mapping.

The automated schema mapping feature helps to identify similar data elements in a database. It can help recognize columns with string data types containing date information. You can then easily access this column to convert the string data to date data type using the Snowflake TO_DATE function. 

  • Incremental Data Load: It ensures proper bandwidth utilization at both the source and the destination by allowing real-time data transfer of the modified data. It improves the query efficiency of the TO_DATE function by avoiding processing unnecessary data. 

Conclusion

This blog provides comprehensive information on the Snowflake TO_DATE function. It explains in detail how to use TO_DATE in Snowflake with examples and how to handle errors while using the function. One prominent step to avoid errors is correctly integrating your data in Snowflake.

You can achieve this using Hevo Data, a zero-code data integration tool. It provides robust transformation and incremental data loading capabilities to streamline your data before applying the TO_DATE conversion.

FAQs

  1. What is the difference between TO_DATE and Try_to_date?

The prime difference between TO_DATE and TRY_TO_DATE is that the latter helps you manage any errors in your queries by giving a NULL value instead of raising an error. 

  1. Does TO_DATE work with timestamp?

TO_DATE works with a timestamp but does not accept the timestamp value inside the VARIANT

Satyam Agrawal
CX Engineer

Satyam boasts over two years of adept troubleshooting and deliverable-oriented experience. His client-focused approach has enabled seamless data pipeline management for numerous SMEs and Enterprises. Proficient in Hevo’s ETL architecture and skilled in DBMS sources, he ensures smooth data movement for clients. Satyam leverages automated tools to extract and load data from various databases to warehouses, implementing SQL principles and API calls for day-to-day troubleshooting.

All your customer data in one place.