Date & time data types¶
Snowflake supports data types for managing dates, times, and timestamps (combined date + time). Snowflake also supports formats for string constants used in manipulating dates, times, and timestamps.
Data types¶
Snowflake supports the following date and time data types:
Note
For DATE and TIMESTAMP data, Snowflake recommends using years between 1582 and 9999. Snowflake accepts some years outside this range, but years prior to 1582 should be avoided due to limitations on the Gregorian Calendar.
DATE¶
Snowflake supports a single DATE data type for storing dates (with no time elements).
DATE accepts dates in the most common forms (YYYY-MM-DD, DD-MON-YYYY, and so on).
In addition, all accepted TIMESTAMP values are valid inputs for dates, but the TIME information is truncated.
DATETIME¶
DATETIME is synonymous with TIMESTAMP_NTZ.
Interval data types¶
Interval data types store values that represent a duration of time. You can calculate an interval as the difference between two dates or times. An interval only defines a duration, so it doesn’t have a start or end point in time. For example, you might define an interval as three years and seven months.
Snowflake supports the following year-month variations of interval data types:
Data type |
Description |
|---|---|
INTERVAL YEAR |
Represents a duration of time in years. |
INTERVAL YEAR TO MONTH |
Represents a duration of time in years and months. |
INTERVAL MONTH |
Represents a duration of time in months. |
Snowflake supports the following day-time variations of interval data types:
Data type |
Description |
|---|---|
INTERVAL DAY |
Represents a duration of time in days. |
INTERVAL DAY TO HOUR |
Represents a duration of time in days and hours. |
INTERVAL DAY TO MINUTE |
Represents a duration of time in days, hours, and minutes. |
INTERVAL DAY TO SECOND |
Represents a duration of time in days, hours, minutes, seconds, and fractional seconds. |
INTERVAL HOUR |
Represents a duration of time in hours. |
INTERVAL HOUR TO MINUTE |
Represents a duration of time in hours and minutes. |
INTERVAL HOUR TO SECOND |
Represents a duration of time in hours, minutes, seconds, and fractional seconds. |
INTERVAL MINUTE |
Represents a duration of time in minutes. |
INTERVAL MINUTE TO SECOND |
Represents a duration of time in minutes, seconds, and fractional seconds. |
INTERVAL SECOND |
Represents a duration of time in seconds and fractional seconds. |
The following sections describe interval data types in more detail:
Note
You can also use interval constants for date and time arithmetic. However, interval constants don’t support interval storage as a column type.
Benefits of interval data types¶
Interval data types provide the following benefits:
Ensure accurate date arithmetic without ambiguity.
Eliminate the need for manual conversion and casting from integer-based durations.
Optimize storage for data that represents intervals of time.
Optimize query execution for duration data.
Simplify the migration of data from third-party databases, such as Databricks, Oracle, and Teradata.
Comply fully with ANSI standards.
Syntax of interval data types¶
To specify an interval data type, use the following syntax:
Where:
Properties:
precisionis the total number of digits that is allowed. Precision can range from1to9.Default:
9fractional_seconds_precisionis the number of digits in the fractional part of a second. Time precision can range from0(seconds) to9(nanoseconds).Default:
9
Use this syntax when you are specifying an interval data type. For example, the following table has a duration
column of INTERVAL YEAR TO MONTH type:
Representing interval values¶
You can represent an interval value by using an interval literal or an interval format:
Interval literals¶
An interval literal is an expression that specifies a duration of time in a string literal. Use the following syntax to specify an interval literal:
Where:
signis an optional symbol that specifies a positive (+) or negative (-) duration of time.Default:
+.stringis a value that represents a time duration.yearMonthQualifieris a qualifier that is defined in Syntax of interval data types.dayTimeQualifieris a qualifier that is defined in Syntax of interval data types.
Interval formats¶
String literals in specific formats can represent interval values.
To specify values for years and months, use the following format:
Where:
signis a required symbol that specifies a positive (+) or negative (-) duration of time.Default:
+.Yis the number of years. The number of digits that is allowed (precision) depends on the data type of the value.MMis two digits for the number of months, from00to11.
To specify values for days, hours, seconds, and fractional seconds, use the following format:
Where:
signis a required symbol that specifies a positive (+) or negative (-) duration of time.Default:
+.Dis the number of days. The number of digits that is allowed (precision) depends on the data type of the value.Omit
Dfor values of the following types:INTERVAL HOUR
INTERVAL HOUR TO MINUTE
INTERVAL HOUR TO SECOND
INTERVAL MINUTE
INTERVAL MINUTE TO SECOND
INTERVAL SECOND
HH24is two digits for the number of hours, from00to23.Omit
HH24for values of the following types:INTERVAL DAY
INTERVAL MINUTE
INTERVAL MINUTE TO SECOND
INTERVAL SECOND
MIis two digits for the number of minutes, from00through59.Omit
MIfor values of the following types:INTERVAL DAY TO HOUR
INTERVAL DAY
INTERVAL HOUR
INTERVAL SECOND
SSis two digits for the number of seconds, from00through59.Omit
SSfor values of the following types:INTERVAL DAY
INTERVAL DAY TO HOUR
INTERVAL DAY TO MINUTE
INTERVAL HOUR
INTERVAL HOUR TO MINUTE
INTERVAL MINUTE
Fis the number of fractional seconds for the data types that include seconds. The number of digits that is allowed (precision) depends on the data type of the value.
The following usage notes apply to string literals in interval format:
The string literal representation applies when you use the CAST or TO_CHAR function to cast intervals explicitly to text strings.
Leading zeros in a field specify precision.
Examples of interval values¶
The following table shows how to represent various interval values. The values shown in the table conform to the following rules for interval values:
For positive values, the plus sign
+is optional for interval literal values but required for interval format values.In the interval literal values, the value in parentheses specifies the precision, which is the number of digits that is allowed. For example,
YEAR(3)specifies that three digits are allowed in the year.In the interval format values, the primary field (the leading field) does not include leading zeros. Subordinate fields use a fixed number of digits. For example, in a YEAR TO MONTH value like
+1-08, the year field has no leading zeros, and the month field uses two digits.
Duration |
Type |
Interval literal value |
Interval format value |
|---|---|---|---|
Positive 5 years |
INTERVAL YEAR |
|
|
Positive 1 year and 8 months |
INTERVAL YEAR TO MONTH |
|
|
Negative 5 months |
INTERVAL MONTH |
|
|
Positive 14 months |
INTERVAL MONTH |
|
|
Negative 44 years and 11 months |
INTERVAL YEAR TO MONTH |
|
|
Positive 11 days, 10 hours, and 9 minutes |
INTERVAL DAY TO MINUTE |
|
|
Positive 2 days, 23 hours, 8 minutes, 23 seconds, and 275 milliseconds |
INTERVAL DAY TO SECOND |
|
|
Positive 4 seconds and 300 milliseconds |
INTERVAL SECOND |
|
|
Operations that involve date and time values¶
The following table shows the data type of the result for valid arithmetic operations that involve interval values:
First operand |
Operator |
Second operand |
Result type |
|---|---|---|---|
Timestamp |
|
Timestamp |
An interval data type |
Date or timestamp |
|
Interval |
DATE, DATETIME, TIMESTAMP_LTZ, TIMESTAMP_NTZ, or TIMESTAMP_TZ |
Date or timestamp |
|
Interval |
DATE, DATETIME, TIMESTAMP_LTZ, TIMESTAMP_NTZ, or TIMESTAMP_TZ |
Interval |
|
Date or timestamp |
DATE, DATETIME, TIMESTAMP_LTZ, TIMESTAMP_NTZ, or TIMESTAMP_TZ |
Numeric |
|
Interval |
An interval data type |
Interval |
|
Numeric |
An interval data type |
Interval |
|
Numeric |
An interval data type |
Interval |
|
Interval |
An interval data type |
Interval |
|
Interval |
An interval data type |
For operations that involve two interval values, the values must both be year-month interval values, or they must both be day-time interval values. Operations that mix year-month interval values and day-time interval values aren’t supported. When the operation involves two year-month interval values, the result type is a year-month interval type. When the operation involves two day-time interval values, the result type is a day-time interval type.
Functions that accept interval values as arguments¶
The following functions accept interval values as arguments:
Examples for interval data types¶
The following examples show how to use interval data types:
Performing arithmetic by using interval data¶
The following examples perform arithmetic by using interval data.
Add one year and one month to a date:
Subtract one year and one month from a date:
Add a period of time to a timestamp:
The following example uses the SYSTEM$TYPEOF function to show that an INTERVAL DAY TO SECOND value is returned when a query subtracts two timestamp values:
To view the results of the query in interval format, you can cast the expression to the INTERVAL DAY(2) TO SECOND(2) data type to specify precision, and then cast to VARCHAR:
Inserting and querying year-month interval data¶
Create a table that tracks candidates for open positions with an INTERVAL YEAR TO MONTH column, and insert data:
When you query the table without casting the duration_of_experience` column to a data type, the output shows the
column values as the total number of months in each row:
When you query the table and cast the duration_of_experience column to the VARCHAR data type, the output shows the
column values in interval format:
Inserting and querying day-time interval data¶
Create a table that specifies the timeout duration for various software features with an INTERVAL HOUR TO SECOND column, and insert data:
Query the table and cast the timeout_duration column to the VARCHAR data type:
Copying interval data into a table and querying the table¶
Complete the following steps to stage a file with interval data, and then copy the file into a table:
In a file on your file system, copy the following content:
This example assumes that the file is named
interval_values.csvin the/examples/intervals/directory.Create a stage:
In the internal staging location, stage the file:
Create a table for the data:
To load the staged file into the table that you created, use the COPY INTO <table> command:
To view the loaded data, query the table, and cast to the VARCHAR type to view the loaded data:
Limitations for interval data types¶
The following limitations apply to interval data types:
Year-month interval values can’t be combined or compared with day-time interval values.
Interval constants and values of interval data type can’t be combined or compared.
Interval constants can’t be inserted into a column that has an interval data type.
VARIANT values can’t contain interval values.
Structured data type values can’t contain interval values.
Interval expressions can’t be used in user-defined functions (UDFs) or Snowflake Scripting.
The following types of tables can’t have interval columns:
Queries on interval columns can’t benefit from the Search optimization service.
TIME¶
Snowflake supports a single TIME data type for storing times in the form of HH:MI:SS.
TIME supports an optional precision parameter for fractional seconds (for example, TIME(3)).
Time precision can range from 0 (seconds) to 9 (nanoseconds). The default precision is 9.
All TIME values must be between 00:00:00 and 23:59:59.999999999. TIME internally stores “wallclock” time, and all operations on TIME values are performed
without taking any time zone into consideration.
TIMESTAMP_LTZ , TIMESTAMP_NTZ , TIMESTAMP_TZ¶
Snowflake supports three variations of timestamp:
- TIMESTAMP_LTZ:
TIMESTAMP_LTZ internally stores UTC values with a specified precision. However, all operations are performed in the current session’s time zone, controlled by the TIMEZONE session parameter.
Synonymous with TIMESTAMP_LTZ:
TIMESTAMPLTZ
TIMESTAMP WITH LOCAL TIME ZONE
- TIMESTAMP_NTZ:
TIMESTAMP_NTZ internally stores “wallclock” time with a specified precision. All operations are performed without taking any time zone into account.
If the output format contains a time zone, the UTC indicator (
Z) is displayed.TIMESTAMP_NTZ is the default for TIMESTAMP.
Synonymous with TIMESTAMP_NTZ:
TIMESTAMPNTZ
TIMESTAMP WITHOUT TIME ZONE
DATETIME
- TIMESTAMP_TZ:
TIMESTAMP_TZ internally stores UTC values together with an associated time zone offset. When a time zone isn’t provided, the session time zone offset is used. All operations are performed with the time zone offset specific to each record.
Synonymous with TIMESTAMP_TZ:
TIMESTAMPTZ
TIMESTAMP WITH TIME ZONE
TIMESTAMP_TZ values are compared based on their times in UTC. For example, the following comparison between different times in different timezones returns TRUE because the two values have equivalent times in UTC.
Attention
TIMESTAMP_TZ currently only stores the offset of a given time zone, not the actual time zone, at the moment of creation for a given value. This is especially important for daylight saving time, which is not utilized by UTC.
For example, with the TIMEZONE parameter set to "America/Los_Angeles", converting a value to TIMESTAMP_TZ in January of a given year stores the
time zone offset of -0800. If six months are later added to the value, the -0800 offset is retained, even though in July the offset for Los Angeles is
-0700. This is because, after the value is created, the actual time zone information ("America/Los_Angeles") is no longer available. The following code
sample illustrates this behavior:
TIMESTAMP¶
TIMESTAMP in Snowflake is a user-specified alias associated with one of the TIMESTAMP_* variations. In all operations where TIMESTAMP is used, the associated TIMESTAMP_* variation is automatically used. The TIMESTAMP data type is never stored in tables.
The TIMESTAMP_* variation associated with TIMESTAMP is specified by the TIMESTAMP_TYPE_MAPPING session parameter. The default is TIMESTAMP_NTZ.
All timestamp variations, as well as the TIMESTAMP alias, support an optional precision parameter for fractional
seconds (for example, TIMESTAMP(3)). Timestamp precision can range from 0 (seconds) to 9 (nanoseconds). The default precision is 9.
Timestamp examples¶
These examples create a table using different timestamps.
First, create a table with a TIMESTAMP column (mapped to TIMESTAMP_NTZ):
Next, explicitly use one of the TIMESTAMP variations (TIMESTAMP_LTZ):
Use TIMESTAMP_LTZ with different time zones:
This query shows that the time for January 2nd is 08:00 in Los Angeles (which is 16:00 in UTC):
Next, note that the times change with a different time zone:
Create a table and use TIMESTAMP_NTZ:
Note that both times from different time zones are converted to the same “wallclock” time:
Next, note that changing the session time zone doesn’t affect the results:
Create a table and use TIMESTAMP_TZ:
Note that the January 1st record inherited the session time zone,
and America/Los_Angeles was converted to a numeric time zone offset:
Next, note that changing the session time zone doesn’t affect the results:
Supported calendar¶
Snowflake uses the Gregorian Calendar for all dates and timestamps. The Gregorian Calendar starts in the year 1582, but recognizes prior years, which is important to note
because Snowflake does not adjust dates prior to 1582 (or calculations involving dates prior to 1582) to match the Julian Calendar. The UUUU format element
supports negative years.
Date and time formats¶
All of these data types accept most non-ambiguous date, time, or date + time formats. See Supported formats for AUTO detection for the formats that Snowflake recognizes when configured to detect the format automatically.
You can also specify the date and time format manually. When specifying the format, you can use the case-insensitive elements listed in the following table:
Format element |
Description |
|---|---|
|
Four-digit [1] year. |
|
Two-digit [1] year, controlled by the TWO_DIGIT_CENTURY_START session parameter. For example, when set to |
|
One-digit or two-digit [2] year without leading zeros, controlled by the TWO_DIGIT_CENTURY_START session parameter. For example, when the parameter set to |
|
Two-digit [1] month ( |
|
One-digit or two-digit [2] month without leading zeros ( |
|
Abbreviated month name [3]. |
|
Full month name [3]. |
|
Two-digit [1] day of month ( |
|
One-digit or two-digit [2] day of month without leading zeros ( |
|
Abbreviated day of week. |
|
Two digits [1] for hour ( |
|
Two digits [1] for hour ( |
|
One or two digits [2] for hour without leading zeros ( |
|
One or two digits [2] for hour without leading zeros ( |
|
Ante meridiem ( |
|
Ante meridiem ( |
|
Synonym for |
|
Synonym for |
|
Two digits [1] for minute ( |
|
One or two digits [2] for minute without leading zeros ( |
|
Two digits [1] for second ( |
|
One or two digits [2] for second without leading zeros ( |
|
Fractional seconds with precision |
|
Two-digit [1] time zone hour and minute, offset from UTC. Can be prefixed by |
|
Four-digit year in ISO format, which are negative for BCE years. |
[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.
Examples of using date and time formats¶
The following example uses FF to indicate that the output has 9 digits in the fractional seconds field:
Date and time constants¶
Constants (also known as literals) are fixed data values. Snowflake supports using string constants to specify fixed date, time, or timestamp values. String constants must always be enclosed between delimiter characters. Snowflake supports using single quotes to delimit string constants.
For example:
The string is parsed as a DATE, TIME, or TIMESTAMP value based on the input format for the data type, as set through the following parameters:
- DATE:
- TIME:
- TIMESTAMP:
For example, to insert a specific date into a column in a table:
Interval constants¶
You can use interval constants to add or subtract a period of time to or from a date, time, or timestamp. Interval constants are implemented using the INTERVAL keyword, which has the following syntax:
As with all string constants, Snowflake requires single quotes to delimit interval constants.
Note
Interval constants support date and time arithmetic, but they don’t support interval storage as a column type. To store interval values in a column, you can use interval data types.
The INTERVAL keyword supports one or more integers and, optionally, one or more date or time parts. For example:
INTERVAL '1 year'represents one year.INTERVAL '4 years, 5 months, 3 hours'represents four years, five months, and three hours.
If a date or time part isn’t specified, the interval represents seconds (for example, INTERVAL '2' is the same
as INTERVAL '2 seconds'). Note that this is different from the default unit of time for performing date arithmetic.
For more details, see Simple arithmetic for dates.
For the list of supported date and time parts, see Supported Date and Time Parts for Intervals.
Note
The order of interval increments is important. The increments are added or subtracted in the order listed. For example:
INTERVAL '1 year, 1 day'first adds or subtracts a year and then a day.INTERVAL '1 day, 1 year'first adds or subtracts a day and then a year.
Ordering differences can affect calculations influenced by calendar events, such as leap years:
INTERVAL is not a data type (that is, you can’t define a table column to be of data type INTERVAL). Intervals can only be used in date, time, and timestamp arithmetic.
You can’t use an interval with a SQL variable. For example, the following query returns an error:
Supported date and time parts for intervals¶
The INTERVAL keyword supports the following date and time parts as arguments (case-insensitive):
Date or Time Part |
Abbreviations / Variations |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Interval examples¶
Add a year interval to a specific date:
Add an interval of 3 hours and 18 minutes to a specific time:
Add a complex interval to the output of the CURRENT_TIMESTAMP function:
The following is sample output. The output is different when the current timestamp is different.
Add a complex interval with abbreviated date/time part notation to a specific date:
Query a table of employee information and return the names of employees who were hired within the past two years and three months:
Filter a TIMESTAMP column named ts from a table named t1 and add four seconds to each returned value:
Simple arithmetic for dates¶
In addition to using interval constants to add to and subtract from dates, times, and timestamps, you can also
add and subtract days to and from DATE values, in the form of { + | - } integer, where integer
specifies the number of days to add or subtract.
Note
TIME and TIMESTAMP values don’t yet support simple arithmetic.
Date arithmetic examples¶
Add one day to a specific date:
Subtract four days from a specific date:
Query a table named employees and return the names of people who left the company, but were employed more than 365 days:
SELECT name FROM employees WHERE end_date > start_date + 365;