Date and time input and output formats

Date and time formats provide a method for representing dates, times, and timestamps.

How Snowflake determines the input and output formats to use

To determine the input and output formats to use for dates, times, and timestamps, Snowflake uses:

Session parameters for dates, times, and timestamps

A set of session parameters determines how date, time, and timestamp data is passed into and out of Snowflake, as well as the time zone used in the time and timestamp formats that support time zones.

You can set the parameters at the account, user, and session levels. Execute the SHOW PARAMETERS command to view the current parameter settings that apply to all operations in the current session.

Input formats

The following parameters define which date, time, and timestamp formats are recognized for DML, including COPY, INSERT, and MERGE operations:

The default for all three parameters is AUTO. When the parameter value is set to AUTO, Snowflake attempts to match date, time, or timestamp strings in any input expression with one of the formats listed in Supported formats for AUTO detection:

  • If a matching format is found, Snowflake accepts the string.

  • If no matching format is found, Snowflake returns an error.

Output formats

The following parameters define the formats for date and time output from Snowflake:

In addition, the following parameter maps the TIMESTAMP data type alias to one of the three TIMESTAMP_* variations:

Time zone

The following parameter determines the time zone:

File format options for loading/unloading dates, times, and timestamps

Separate from the input and output format parameters, Snowflake provides three file format options to use when loading data into or unloading data from Snowflake tables:

  • DATE_FORMAT

  • TIME_FORMAT

  • TIMESTAMP_FORMAT

The options can be specified directly in the COPY command or in a named stage or file format object referenced in the COPY command. When specified, these options override the corresponding input formats (when loading data) or output formats (when unloading data).

Data loading

When used in data loading, the options specify the format of the date, time, and timestamp strings in your staged data files. The options override the DATE_INPUT_FORMAT, TIME_INPUT_FORMAT, or TIMESTAMP_INPUT_FORMAT parameter settings.

The default for all these options is AUTO, meaning the COPY INTO <table> command attempts to match all date and timestamp strings in the staged data files with one of the formats listed in Supported formats for AUTO detection:

  • If a matching format is found, Snowflake accepts the string.

  • If no matching format is found, Snowflake returns an error and then performs the action specified for the ON_ERROR copy option.

Warning

Snowflake supports automatic detection of most common date, time, and timestamp formats (see tables below). However, some formats might produce ambiguous results, which can cause Snowflake to apply an incorrect format when using AUTO for data loading.

To guarantee correct loading of data, Snowflake strongly recommends explicitly setting the file format options for data loading.

Data unloading

When used in data unloading, the options specify the format applied to the dates, times, and timestamps unloaded to the files in specified stage.

The default for all these options is AUTO, meaning Snowflake applies the formatting specified in the following parameters:

  • DATE_OUTPUT_FORMAT

  • TIME_OUTPUT_FORMAT

  • TIMESTAMP_*_OUTPUT_FORMAT (depending on the TIMESTAMP_TYPE_MAPPING setting)

About the elements used in input and output formats

In input and output formats that you specify in parameters, file format options, and conversion functions, you can use the elements listed in the table below.

The next sections also use these elements to describe the formats recognized by Snowflake automatically.

[1] The number of digits describes the output produced when serializing values to text. When parsing text, Snowflake accepts up to the specified number of digits. For example, a day number can be one or two digits.

[2] The number of digits describes the output produced when serializing values to text. Parsing isn’t supported. If parsing is required, use an equivalent format that includes leading zeros.

[3] For the MON format element, the output produced when serializing values to text is the abbreviated month name. For the MMMM format element, the output produced when serializing values to text is the full month name. When parsing text, Snowflake accepts the three-digit abbreviation or the full month name for both MON and MMMM. For example, “January” or “Jan”, “February” or “Feb”, and so on are accepted when parsing text.

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. Snowflake recommends always enclosing literal characters in double quotes (for example, "T", "EST", "Z") to ensure they are treated as literals.

  • For more details about valid ranges, number of digits, and best practices, see Additional information about using date, time, and timestamp formats.

Supported formats for AUTO detection

If instructed to do so, Snowflake automatically detects and processes specific formats for date, time, and timestamp input strings. The following sections describe the supported formats:

Attention

Some strings can match multiple formats. For example, '07-04-2016' is compatible with both MM-DD-YYYY and DD-MM-YYYY, but has different meanings in each format (July 4 vs. April 7). The fact that a matching format is found does not guarantee that the string is parsed as the user intended.

Although automatic date format detection is convenient, it increases the possibility of misinterpretation. Snowflake strongly recommends specifying the format explicitly rather than relying on automatic date detection.

Date formats

For descriptions of the elements used in the formats below, see About the elements used in input and output formats.

When using AUTO date formatting, dashes and slashes aren’t interchangeable. Slashes imply MM/DD/YYYY format, and dashes imply YYYY-MM-DD format. Strings such as '2019/01/02' or '01-02-2019' aren’t interpreted as you might expect.

Time formats

For descriptions of the elements used in the formats below, see About the elements used in input and output formats.

The AM format element allows values with either AM or PM.

Note

Use the AM format element only with HH12 (not with HH24).

When a timezone offset (for example, 0800) occurs immediately after a digit in a time or timestamp string, the timezone offset must start with + or -. The sign prevents ambiguity when the fractional seconds or the time zone offset does not contain the maximum number of allowable digits. For example, without a separator between the last digit of the fractional seconds and the first digit of the timezone, the 1 in the time 04:04:04.321200 could be either the last digit of the fractional seconds (that is, 321 milliseconds) or the first digit of the timezone offset (that is, 12 hours ahead of UTC).

Timestamp formats

For descriptions of the elements used in the formats below, see About the elements used in input and output formats.

When a timezone offset (for example, 0800) occurs immediately after a digit in a time or timestamp string, the timezone offset must start with + or -. The sign prevents ambiguity when the fractional seconds or the time zone offset does not contain the maximum number of allowable digits. For example, without a separator between the last digit of the fractional seconds and the first digit of the timezone, the 1 in the time 04:04:04.321200 could be either the last digit of the fractional seconds (that is, 321 milliseconds) or the first digit of the timezone offset (that is, 12 hours ahead of UTC).

Tip

In some of the timestamp formats, the letter T is used as a separator between the date and time (for example, 'YYYY-MM-DD"T"HH24:MI:SS').

The double quotes around the T are optional. However, Snowflake recommends always enclosing literal characters — such as T, timezone abbreviations like EST, or any other non-element text — in double quotes to avoid ambiguity.

Use the double quotes only in the format specifier, not the actual values. For example:

-- Not recommended: literals T and EST are not quoted
SELECT TO_TIMESTAMP('2026-01-02T03:04:05 EST', 'YYYY-MM-DDTHH24:MI:SS EST');

-- Recommended: all literal characters are enclosed in double quotes
SELECT TO_TIMESTAMP('2026-01-02T03:04:05 EST', 'YYYY-MM-DD"T"HH24:MI:SS "EST"');

The quotes around literal characters must be double quotes, not single quotes.

Additional information about using date, time, and timestamp formats

The following sections describe requirements and best practices for individual fields in dates, times, and timestamps.

Valid ranges of values for fields

The recommended ranges of values for each field are shown below:

Using the correct number of digits with format elements

For most fields (year, month, day, hour, minute, and second), the elements (YYYY, MM, DD, and so on) of the format specifier are two or four characters.

The following rules tell you how many digits you should actually specify in the literal values:

  • YYYY: You can specify 1, 2, 3, or 4 digits of the year. However, Snowflake recommends specifying 4 digits. If necessary, prepend leading zeros. For example, the year 536 A.D. is 0536.

  • YY: Specify 1 or 2 digits of the year. However, Snowflake recommends specifying 2 digits. If necessary, prepend a leading zero.

  • MM: Specify one or two digits. For example, January can be represented as 01 or 1. Snowflake recommends using two digits.

  • DD: Specify one or two digits. Snowflake recommends using two digits.

  • HH12 and HH24: Specify one or two digits. Snowflake recommends using two digits.

  • MI: Specify one or two digits. Snowflake recommends using two digits.

  • SS: Specify one or two digits. Snowflake recommends using two digits.

  • FF9: Specify between 1 and 9 digits (inclusive). Snowflake recommends specifying the number of actual significant digits. Trailing zeros aren’t required.

  • TZH: Specify one or two digits. Snowflake recommends using two digits.

  • TZM: Specify one or two digits. Snowflake recommends using two digits.

For all fields (other than fractional seconds), Snowflake recommends specifying the maximum number of digits. Use leading zeros if necessary. For example, 0001-02-03 04:05:06 -07:00 follows the recommended format.

For fractional seconds, trailing zeros are optional. In general, it is considered good practice to specify only the number of digits that are reliable and meaningful. For example, if a time measurement is accurate to three decimal places (milliseconds), then specifying it as nine digits (for example, .123000000) might be misleading.

Whitespace in values and format specifiers

Snowflake enforces matching whitespace in some, but not all, situations. For example, the following statement generates an error because there is no space between the days and the hours in the specified value, but there is a space between DD and HH in the format specifier:

SELECT TO_TIMESTAMP('2019-02-2823:59:59 -07:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM');

However, the following statement doesn’t generate an error, even though the value contains a whitespace where the specifier doesn’t:

SELECT TO_TIMESTAMP('2019-02-28 23:59:59.000000000 -07:00', 'YYYY-MM-DDHH24:MI:SS.FF TZH:TZM');

The reason for the difference is that in the former case, the values would be ambiguous if the fields aren’t all at their maximum width. For example, 213 could be interpreted as 2 days and 13 hours, or as 21 days and 3 hours. However, DDHH is unambiguously the same as DD HH (other than the whitespace).

Tip

Although some whitespace differences are allowed in order to handle variably-formatted data, Snowflake recommends that values and specifiers exactly match, including spaces.

Context dependency

Not all restrictions are enforced equally in all contexts. For example, some expressions might roll over February 31, while others might not.

Summary of best practices for specifying the format

These best practices minimize ambiguities and other potential issues in past, current, and projected future versions of Snowflake:

  • Be aware of the dangers of mixing data from sources that use different formats (for example, of mixing data that follows the common U.S. format MM-DD-YYYY and the common European format DD-MM-YYYY).

  • Specify the maximum number of digits for each field (except fractional seconds). For example, use four-digit years, specifying leading zeros if necessary.

  • Specify a blank or the letter T between the date and time in a timestamp. If using T, Snowflake recommends writing it as "T" in the format string.

  • Enclose literal characters in double quotes within format strings. For example, use YYYY-MM-DD"T"HH24:MI:SS "EST" rather than YYYY-MM-DDTHH24:MI:SS EST. This helps prevent literal text from being misinterpreted as format elements.

  • Make sure whitespace (and the "T" separator between the date and time) are the same in values and in the format specifier.

  • Use interval arithmetic if you need the equivalent of rollover.

  • Be careful when using AUTO formatting. When possible, specify the format, and ensure that values always match the specified format.

  • Specify the format in the command, because it is safer than specifying the format outside the command, for example in a parameter such as DATE_INPUT_FORMAT. (See below.)

  • When moving scripts from one environment to another, ensure that date-related parameters, such as DATE_INPUT_FORMAT, are the same in the new environment as they were in the old environment (assuming that the values are also in the same format).

Date & time functions

Snowflake provides a set of functions to construct, convert, extract, or modify DATE, TIME, and TIMESTAMP data. For more information, see Date & Time Functions.

AUTO detection of integer-stored date, time, and timestamp values

For integers of seconds or milliseconds stored in a string, Snowflake attempts to determine the correct unit of measurement based on the length of the value.

Note

The use of quoted integers as inputs is deprecated.

This example calculates the timestamp equivalent to 1487654321 seconds since the start of the Unix epoch:

SELECT TO_TIMESTAMP('1487654321');
+-------------------------------+
| TO_TIMESTAMP('1487654321')    |
|-------------------------------|
| 2017-02-21 05:18:41.000000000 |
+-------------------------------+

Here is a similar calculation using milliseconds since the start of the epoch:

SELECT TO_TIMESTAMP('1487654321321');
+-------------------------------+
| TO_TIMESTAMP('1487654321321') |
|-------------------------------|
| 2017-02-21 05:18:41.321000000 |
+-------------------------------+

Depending on the magnitude of the value, Snowflake uses a different unit of measure:

  • After the string is converted to an integer, the integer is treated as a number of seconds, milliseconds, microseconds, or nanoseconds after the start of the Unix epoch (1970-01-01 00:00:00.000000000 UTC).

    • If the integer is less than 31536000000 (the number of milliseconds in a year), then the value is treated as a number of seconds.

    • If the value is greater than or equal to 31536000000 and less than 31536000000000, then the value is treated as milliseconds.

    • If the value is greater than or equal to 31536000000000 and less than 31536000000000000, then the value is treated as microseconds.

    • If the value is greater than or equal to 31536000000000000, then the value is treated as nanoseconds.

  • If more than one row is evaluated (for example, if the input is the column name of a table that contains more than one row), each value is examined independently to determine if the value represents seconds, milliseconds, microseconds, or nanoseconds.

In cases where formatted strings and integers in strings are passed to the function, each value is cast according to the contents of the string. For example, if you pass a date-formatted string and a string containing an integer to TO_TIMESTAMP, the function interprets each value correctly according to what each string contains:

SELECT TO_TIMESTAMP(column1) FROM VALUES ('2013-04-05'), ('1487654321');
+-------------------------+
| TO_TIMESTAMP(COLUMN1)   |
|-------------------------|
| 2013-04-05 00:00:00.000 |
| 2017-02-21 05:18:41.000 |
+-------------------------+

Date & time function format best practices

AUTO detection usually determines the correct input format. However, there are situations where it might not be able to make the correct determination.

To avoid this, Snowflake strongly recommends the following best practices (substituting TO_DATE , DATE or TO_TIME , TIME for TO_TIMESTAMP, as appropriate):

  • Avoid using AUTO format if there is any chance for ambiguous results. Instead, specify an explicit format string by:

  • For strings containing integer values, specify the scale using the following syntax:

    TO_TIMESTAMP(TO_NUMBER(<string_column>), <scale>)