A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/functions/to_date below:

Website Navigation


TO_DATE , DATE | Snowflake Documentation

Categories:

Conversion functions , Date & time functions

TO_DATE , DATE

Converts an input expression to a date:

For all other values, a conversion error is generated.

See also:

TRY_TO_DATE

Syntax
TO_DATE( <string_expr> [, <format> ] )
TO_DATE( <timestamp_expr> )
TO_DATE( '<integer>' )
TO_DATE( <variant_expr> )

DATE( <string_expr> [, <format> ] )
DATE( <timestamp_expr> )
DATE( '<integer>' )
DATE( <variant_expr> )

Copy

Arguments

Required:

One of:

string_expr

String from which to extract a date. For example: '2024-01-31'.

timestamp_expr

A TIMESTAMP expression. The DATE portion of the TIMESTAMP value is extracted.

'integer'

An expression that evaluates to a string containing an integer. For example: '15000000'. Depending on the magnitude of the string, it can be interpreted as seconds, milliseconds, microseconds, or nanoseconds. For details, see the Usage notes for this function.

variant_expr

An expression of type VARIANT.

The VARIANT must contain one of the following:

  • A string from which to extract a date.

  • A date.

  • A string containing an integer that represents the number of seconds or milliseconds.

Although TO_DATE accepts a TIMESTAMP value, it does not accept a TIMESTAMP value inside a VARIANT.

Optional:

format

Date format specifier for string_expr or AUTO, which specifies that Snowflake automatically detects the format to use. For more information, see Date and time formats in conversion functions.

The default is the current value of the DATE_INPUT_FORMAT session parameter (default AUTO).

Returns

The data type of the returned value is DATE. If the input is NULL, returns NULL.

Usage notes Examples

The following examples use the TO_DATE and DATE functions.

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

Copy

+-----------------------+--------------------+
| TO_DATE('2024-05-10') | DATE('2024-05-10') |
|-----------------------+--------------------|
| 2024-05-10            | 2024-05-10         |
+-----------------------+--------------------+
Examples that use different input formats

The following examples use the TO_DATE and DATE functions with different input format specifications. The date format in the returned output is determined by the setting of the DATE_OUTPUT_FORMAT session parameter.

SELECT TO_DATE('2024.05.10', 'YYYY.MM.DD'), DATE('2024.05.10', 'YYYY.MM.DD');

Copy

+-------------------------------------+----------------------------------+
| TO_DATE('2024.05.10', 'YYYY.MM.DD') | DATE('2024.05.10', 'YYYY.MM.DD') |
|-------------------------------------+----------------------------------|
| 2024-05-10                          | 2024-05-10                       |
+-------------------------------------+----------------------------------+
SELECT TO_DATE('2024-05-10', 'AUTO'), DATE('2024-05-10', 'AUTO');

Copy

+-------------------------------+----------------------------+
| TO_DATE('2024-05-10', 'AUTO') | DATE('2024-05-10', 'AUTO') |
|-------------------------------+----------------------------|
| 2024-05-10                    | 2024-05-10                 |
+-------------------------------+----------------------------+
SELECT TO_DATE('05/10/2024', 'MM/DD/YYYY'), DATE('05/10/2024', 'MM/DD/YYYY');

Copy

+-------------------------------------+----------------------------------+
| TO_DATE('05/10/2024', 'MM/DD/YYYY') | DATE('05/20/2024', 'MM/DD/YYYY') |
|-------------------------------------+----------------------------------|
| 2024-05-10                          | 2024-05-20                       |
+-------------------------------------+----------------------------------+
Examples that use different output formats

The following examples show the results of queries when the DATE_OUTPUT_FORMAT session parameter is set to DD-MON-YYYY:

ALTER SESSION SET DATE_OUTPUT_FORMAT = 'DD-MON-YYYY';

Copy

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

Copy

+-------------------------------------+----------------------------------+
| TO_DATE('2024-05-10', 'YYYY-MM-DD') | DATE('2024-05-10', 'YYYY-MM-DD') |
|-------------------------------------+----------------------------------|
| 10-May-2024                         | 10-May-2024                      |
+-------------------------------------+----------------------------------+
SELECT TO_DATE('05/10/2024', 'MM/DD/YYYY'), DATE('05/10/2024', 'MM/DD/YYYY');

Copy

+-------------------------------------+----------------------------------+
| TO_DATE('05/10/2024', 'MM/DD/YYYY') | DATE('05/10/2024', 'MM/DD/YYYY') |
|-------------------------------------+----------------------------------|
| 10-May-2024                         | 10-May-2024                      |
+-------------------------------------+----------------------------------+
Examples that use a string that contains an integer

When the input is a string that contains an integer, the magnitude of that integer affects whether it is interpreted as seconds, milliseconds, etc. The following example shows how the function chooses the units to use (seconds, milliseconds, microseconds, or nanoseconds), based on the magnitude of the value.

Create and load the table:

CREATE OR REPLACE TABLE demo1 (
  description VARCHAR,
  value VARCHAR -- string rather than bigint
);

INSERT INTO demo1 (description, value) VALUES
  ('Seconds',      '31536000'),
  ('Milliseconds', '31536000000'),
  ('Microseconds', '31536000000000'),
  ('Nanoseconds',  '31536000000000000');

Copy

Pass the strings to the function:

SELECT description,
       value,
       TO_TIMESTAMP(value),
       TO_DATE(value)
  FROM demo1
  ORDER BY value;

Copy

+--------------+-------------------+-------------------------+----------------+
| DESCRIPTION  | VALUE             | TO_TIMESTAMP(VALUE)     | TO_DATE(VALUE) |
|--------------+-------------------+-------------------------+----------------|
| Seconds      | 31536000          | 1971-01-01 00:00:00.000 | 1971-01-01     |
| Milliseconds | 31536000000       | 1971-01-01 00:00:00.000 | 1971-01-01     |
| Microseconds | 31536000000000    | 1971-01-01 00:00:00.000 | 1971-01-01     |
| Nanoseconds  | 31536000000000000 | 1971-01-01 00:00:00.000 | 1971-01-01     |
+--------------+-------------------+-------------------------+----------------+

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4