Date Functions
- Capella Operational
- reference
SQL++ date functions return the system clock value or manipulate the datetime values, which are represented as a string or an integer. These functions are very useful for manipulating dates in datasets with various date formats and timezones.
Timezones
Datetime values are always tied to a specific timezone, either explicitly in the date value, or implicitly in the application. The date functions in SQL++ therefore support multiple different timezones.
UTC
UTC, The Coordinated Universal Time is the primary time standard by which the world regulates clocks and time. It is defined as the time at 0° longitude and is consistent, as it does not take into account daylight savings time. You can read further about UTC at https://www.timeanddate.com/time/aboututc.html.
All SQL++ functions which accept a timezone as an argument also accept UTC
.
IANA Timezones
Many applications operate across multiple different time zones and may not necessarily use UTC
.
Therefore, it is important for the database to be able to handle and manipulate dates in these time zones in a consistent manner.
Many date functions take the time zone as an additional argument.
Timezones are case sensitive, Europe/London is not the same as europe/london .
|
It is important to note that many time zones change their UTC offset based on daylight savings time, as a result the UTC offset of times may change based on the time of year. SQL++ take this into account when converting dates.
Below are a few examples of commonly used timezones and their offsets:
Timezone | UTC Offset (without daylight savings time) | UTC Offset (during daylight savings time) |
---|---|---|
America/New_York |
-05:00 |
-04:00 |
America/Tijuana |
-08:00 |
-07:00 |
Europe/Paris |
+01:00 |
+02:00 |
Europe/London |
+00:00 |
+01:00 |
Asia/Tel_Aviv |
+02:00 |
+03:00 |
Asia/Kolkata |
+05:30 |
+05:30 |
Date Formats
SQL++ date functions accept dates in either Epoch/UNIX timestamp format or string date format.
SQL++ is then able to represent the passed date as a standardized date object internally.
In general, functions whose name contains the word STR
are designed to use string formats while MILLIS
functions are designed to use Epoch/UNIX timestamps.
Epoch/UNIX Timestamps
Epoch/UNIX time is the number of seconds (or milliseconds) that have elapsed since 1970-01-01T00:00:00.000Z
(Thursday, 1 January 1970 at midnight), not including leap seconds.
This can be useful for numeric and timezone agnostic representations of dates.
While Epoch/UNIX time can be represented in either seconds or milliseconds, all SQL++ date functions specifically treat Epoch/UNIX timestamps as milliseconds.
For example, the date 2017-01-31T10:02:07Z
would equate to an Epoch/UNIX timestamp of 1485856927000
.
Date String Formats
In many cases, dates are not stored as Epoch/UNIX timestamp but instead as more human-readable formats, such as 2006-01-02T15:04:05.567+08:00
.
Therefore, SQL++ also provides convenience methods to allow you to manipulate and convert dates in string format.
SQL++ accepts format strings following several conventions:
-
ISO-8601 example dates, e.g.
1111-11-11
. -
Date string component codes, e.g.
YYYY-MM-DD
. -
Go language native dates, e.g. specifically
2006-01-02
for year, month, and day. -
Percent-style date format specifiers, e.g.
%Y-%m-%d
.
Only a single style can be used at a time in a specified format string.
-
ISO-8601 Dates
-
Date String Codes
-
Go Reference Dates
-
Percent-Style Dates
ISO-8601 example dates are composed of the following date components.
Component | Code | Value | ||
---|---|---|---|---|
Year |
YYYY |
Any four-digit integer from |
||
Month (of the year) |
MM |
Any two digit integer from |
||
Day (of the month) |
DD |
Any two digit integer from |
||
Hour (of the day) |
hh |
Any two-digit integer from |
||
Minute (of the hour) |
mm |
Any two-digit integer from |
||
Second (of the minute) |
ss |
Any two-digit integer from |
||
Millisecond (of the second) — output only |
s |
Any three-digit integer from |
||
Time Zone (as UTC offset) |
TZD |
UTC offset in the format ±hh:mm |
||
|
To specify a date format, you must put together example component values, as specified above, to create one of the following date formats. ISO-8601 date formats are very specific; they must contain the correct components in the correct order, with punctuation exactly as shown.
Format | Example |
---|---|
YYYY-MM-DDThh:mm:ss.sTZD |
|
YYYY-MM-DDThh:mm:ssTZD |
|
YYYY-MM-DDThh:mm:ss.s |
|
YYYY-MM-DDThh:mm:ss |
|
YYYY-MM-DD hh:mm:ss.sTZD |
|
YYYY-MM-DD hh:mm:ssTZD |
|
YYYY-MM-DD hh:mm:ss.s |
|
YYYY-MM-DD hh:mm:ss |
|
YYYY-MM-DD |
|
hh:mm:ss.sTZD |
|
hh:mm:ssTZD |
|
hh:mm:ss.s |
|
hh:mm:ss |
|
The examples above use arbitrary values for the date components. You can use any valid values in your date components, as long as the date format contains the correct combination of components and punctuation.
Note, however, that if you use Go reference date values as the date components, the example date is interpreted as a Go reference date, rather than an ISO-8601 example date.
This may cause some date formats to be interpreted differently to what you expect.
For example, the date format 2006-02-01
is interpreted as a Go reference date, where 02
is the day and 01
is the month.
For greater flexibility, you can specify a date format using date string codes. These are based on the alphabetic format codes from the ISO-8601 standard, with some extensions. The date string codes are given below.
Code | Component |
---|---|
|
2-digit century |
|
4-digit century and year |
|
2-digit year [note] |
|
2-digit month |
|
2-digit day |
|
2-digit hour, 00-23 |
|
2-digit hour, 00-23 |
|
2-digit minute, 00-59 |
|
2-digit second, 00-59 |
|
Fraction of a second (down to millisecond) — output only |
|
Time Zone (as UTC offset) |
To specify a date format, you can put the date string components together in any order, along with any other characters as required.
Characters which are not part of the format specification are matched literally and produced unaltered, with the exception of Unicode U+0020, i.e. space " "
, which matches any single character when parsing, and is produced unaltered on output.
For example, YYYY MM DD
parses 2021-06-28
, 2021/06/28
, 2021.06.28
, etc.
You can specify a date format using Go language date components. The available reference date components are given below.
Component | Reference Date | Meaning |
---|---|---|
Year |
|
4-digit century and year |
|
2-digit year [note] |
|
Month |
|
Unpadded month |
|
Zero-padded 2-digit month |
|
|
Full English month name |
|
|
Abbreviated English month name |
|
Day |
|
Unpadded day of the month |
|
Space-padded 2-digit day of the month |
|
|
Zero-padded 2-digit day of the month |
|
|
Full English day name |
|
|
Abbreviated English day name |
|
|
Space-padded 3-digit day of the year |
|
|
Zero-padded 3-digit day of the year |
|
Hour |
|
Unpadded hour, 1-12 |
|
Zero-padded 2-digit hour, 01-12 |
|
|
Zero-padded 2-digit hour, 00-23 |
|
AM or PM |
|
AM or PM (upper case) |
|
am or pm (lower case) |
|
Minute |
|
Unpadded minute, 0-59 |
|
Zero-padded minute, 00-59 |
|
Second |
|
Unpadded second, 0-59 |
|
Zero-padded second, 00-59 |
|
Fraction of a second |
|
Fraction of a second, to the given number of decimal places |
|
Fraction of a second, to the given number of decimal places, with trailing zeros removed |
|
Time zone |
|
Time Zone (as UTC offset) |
|
Time Zone (as UTC offset); |
To specify a date format, you can put the reference date components together in any order, along with any other characters as required.
Characters which are not part of the format specification are matched literally and produced unaltered, with the exception of Unicode U+0020, i.e. space " "
, which matches any single character when parsing, and is produced unaltered on output.
For example, 2006 01 02
parses 2021-06-28
, 2021/06/28
, 2021.06.28
, etc.
Date and time functions also accept printf
-style format specifiers for date formats, based on the Unix date command.
Format specifiers begin with a percent character %
and take the following form:
format-specifier ::= '%' ( '%' | ( '-' | '_' | '0' | '^' )? width? element)
The optional hyphen (-
), underscore (_
), or zero (0
) characters specify the padding for number fields.
hyphen -
|
No padding for numeric date components. |
underscore _
|
Padding with spaces for numeric date components. |
zero 0
|
Padding with zeros for numeric date components — the default. |
The optional circumflex (^
) character specifies case insensitivity when parsing text date components, or a preference for upper case when outputting text date components.
The width is accepted but ignored for parsing, and is used for output. However, it should rarely be needed, as elements have common or expected default widths.
The element is a single character which specifies a date component or an entire date format. The elements are given in the table below.
Element | Meaning | Example |
---|---|---|
|
Short form date, YYYY-MM-DD |
|
|
Long form date, YYYY-MM-DDThh:mm:ss.sTZD |
|
|
4-digit century and year |
|
|
2-digit century |
|
|
2-digit year [note] |
|
|
2-digit month |
|
|
Full English month name |
|
|
Abbreviated English month name |
|
|
2-digit day |
|
|
Full English day name |
|
|
Abbreviated English day name |
|
|
English ordinal number suffix |
|
|
2-digit hour, 00-23 |
|
|
2-digit hour, 01-12 |
|
|
AM or PM (upper case) |
|
|
am or pm (lower case) |
|
|
2-digit minute, 00-59 |
|
|
2-digit second, 00-59 |
|
|
24-hour hour and minute (same as |
|
|
24-hour time (same as |
|
|
Fraction of a second (down to nanosecond) |
|
|
When parsing, matches a time zone in any supported format — ±hh, ±hhmm, ±hh:mm, zone name |
|
For output, produces time zone in ±hh:mm format |
|
|
|
When parsing, matches a time zone in any supported format — ±hh, ±hhmm, ±hh:mm, zone name |
|
For output, produces time zone name |
|
|
|
Seconds since 1970-01-01 00:00:00 UTC |
|
To specify a date format, you can put the format specifiers together in any order, along with any other characters as required.
If you need to include a literal percent symbol in the date format, use the special format specifier %%
.
Characters which are not part of the format specification are matched literally and produced unaltered, with the exception of Unicode U+0020, i.e. space " "
, which matches any single character when parsing, and is produced unaltered on output.
For example, %Y %m %d
parses 2021-06-28
, 2021/06/28
, 2021.06.28
, and so on.
Default Values
If the date string does not explicitly declare the value of a component, then the following default values are assumed:
In cases where the timezone is not specified, the local system time is assumed. For example, |
Manipulating Date Components
Dates are composed of multiple different components such as the day, year, month, etc.
It is important for applications to be able to extract and manipulate particular components of a date, so that these can be used in SQL++ queries.
Functions such as DATE_ADD_STR() accept a part
argument, which is the component to adjust.
The following are the supported date parts that can be passed to date manipulation functions.
These date parts are expressed as strings and are not case-sensitive, so year
is regarded the same as YeAr
.
For all examples, the date being used is 2006-01-02T15:04:05.999Z
.
Component | Description | Lower Bound | Upper Bound | Example |
---|---|---|---|---|
|
The millennium (1000 year period), counting from the start of year 0, which is equivalent to 1 BCE. |
- |
- |
3 |
|
The century (100 year period), counting from the start of year 0, which is equivalent to 1 BCE. |
- |
- |
21 |
|
The decade (10 year period), counting from the start of year 0, which is equivalent to 1 BCE.
This is calculated as |
- |
- |
200 |
|
The proleptic Gregorian year. The year 0 is equivalent to 1 BCE. |
- |
- |
2006 |
|
The ISO-8601 year.
Each ISO-8601 year begins with the Monday of the week containing the 4th of January, so in early January and late December the ISO year may differ from the Gregorian year.
Should be used in conjunction with |
- |
- |
2006 |
|
The number of the quarter (3 month period) of the year. January-March (inclusive) is 1 while October-December (inclusive) is 4. |
1 |
4 |
1 |
|
The number of the month of the year. January is 1 and December is 12. |
1 |
12 |
1 |
|
The number of the week of the year. This is the ceiling value of the day of the year divided by 7. |
1 |
53 |
1 |
|
The number of the week of the year, based on the ISO definition.
ISO weeks start on Mondays and the first week of a year contains January 4 of that year.
In other words, the first Thursday of a year will always be in week 1 of that year.
This results in some different results between For example the |
1 |
53 |
1 |
|
The day of the month. |
1 |
31 |
2 |
|
The day of the year. |
1 |
366 |
2 |
|
The day of the week. |
0 |
6 |
1 |
|
The hour of the day. |
0 |
23 |
5 |
|
The minute of the hour. |
0 |
59 |
4 |
|
The second of the minute. |
0 |
59 |
5 |
|
The millisecond of the second. |
0 |
999 |
999 |
|
The offset from UTC in seconds. |
-43200 |
43200 |
0 |
|
The hour component of the offset from UTC. |
-12 |
12 |
0 |
|
The minute component of the offset from UTC. |
-59 |
59 |
0 |
Date Functions
Below is a list of all date functions that SQL++ provides.
If any arguments to any of the following functions are MISSING then the result is also MISSING (i.e.
no result is returned).
Similarly, if any of the arguments are NULL then NULL is returned.
|
CLOCK_LOCAL([fmt])
Description
The current time (at function evaluation time) of the machine that the query service is running on, in the specified string format.
Arguments
- fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If no format or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
CLOCK_LOCAL()
cannot be used as part of an index definition, this includes the indexed fields and the WHERE
clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run.
To avoid inconsistencies between multiple calls to CLOCK_LOCAL()
within a single query, use NOW_LOCAL() instead.
CLOCK_MILLIS()
Description
The current time as an Epoch/UNIX timestamp. Its fractional part represents nanoseconds, but the additional precision beyond milliseconds may not be consistent or guaranteed on all platforms.
Return Value
A single float value (with 3 decimal places) representing the system time as Epoch/UNIX time.
Limitations
CLOCK_MILLIS()
cannot be used as part of an index definition, this includes the indexed fields and the WHERE
clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run.
To avoid inconsistencies between multiple calls to CLOCK_MILLIS()
within a single query, use NOW_MILLIS() instead.
CLOCK_STR([fmt])
Description
The current time (at function evaluation time) of the machine that the query service is running on, in the specified string format.
Arguments
- fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as. .
Optional argument. If no format or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
CLOCK_STR()
cannot be used as part of an index definition, this includes the indexed fields and the WHERE
clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run.
To avoid inconsistencies between multiple calls to CLOCK_STR()
within a single query, use NOW_STR() instead.
CLOCK_TZ(tz [, fmt])
Description
The current time (at function evaluation time) in the timezone given by the timezone argument passed to the function. This time is the local system time converted to the specified timezone.
Arguments
- tz
-
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to.
If this argument is not a valid timezone then
null
is returned as the result. - fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If no format or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
An date string in the format specified representing the system time in the specified timezone.
Limitations
As this function converts the local time, it may not accurately represent the true time in that timezone.
CLOCK_TZ()
cannot be used as part of an index definition, this includes the indexed fields and the WHERE
clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run.
To avoid inconsistencies between multiple calls to CLOCK_TZ()
within a single query, use NOW_TZ() instead.
Examples
SELECT CLOCK_TZ('UTC') as UTC_full_date,
CLOCK_TZ('UTC', '1111-11-11') as UTC_short_date,
CLOCK_TZ('invalid timezone') as invalid_timezone,
CLOCK_TZ('US/Eastern') as us_east,
CLOCK_TZ('US/Pacific') as us_west;
[
{
"UTC_full_date": "2018-01-23T21:54:37.178Z",
"UTC_short_date": "2018-01-23",
"invalid_timezone": null,
"us_east": "2018-01-23T16:54:37.18-05:00",
"us_west": "2018-01-23T13:54:37.181-08:00"
}
]
CLOCK_UTC([fmt])
Description
The current time in UTC.
This time is the local system time converted to UTC.
This function is provided for convenience and is the same as CLOCK_TZ('UTC')
.
Arguments
- fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If no format or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
As this function converts the local time, it may not accurately represent the true time in UTC.
CLOCK_UTC()
cannot be used as part of an index definition, this includes the indexed fields and the WHERE
clause of the index.
If this function is called multiple times within the same query then the values returned may differ, particularly if the query takes a long time to run.
To avoid inconsistencies between multiple calls to CLOCK_UTC()
within a single query, use NOW_UTC() instead.
DATE_ADD_MILLIS(date1, n, part)
Description
Performs date arithmetic on a particular component of an Epoch/UNIX timestamp value.
This calculation is specified by the arguments n
and part
.
+
For example, a value of 3 for n
and a value of day
for part
would add 3 days to the date specified by date1
.
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing an Epoch/UNIX timestamp in milliseconds.
If this argument is not an integer then
null
is returned. - n
-
The value to increment the date component by. This value must be an integer, or any valid expression which evaluates to an integer, and may be negative to perform date subtraction.
If a non-integer is passed to the function then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function then
null
is returned.
Return Value
An integer, representing the result of the calculation as an Epoch/UNIX timestamp in milliseconds.
Examples
SELECT DATE_ADD_MILLIS(1463284740000, 3, 'day') as add_3_days,
DATE_ADD_MILLIS(1463284740000, 3, 'year') as add_3_years,
DATE_ADD_MILLIS(1463284740000, -3, 'day') as sub_3_days,
DATE_ADD_MILLIS(1463284740000, -3, 'year') as sub_3_years;
[
{
"add_3_days": 1463543940000,
"add_3_years": 1557892740000,
"sub_3_days": 1463025540000,
"sub_3_years": 1368590340000
}
]
DATE_ADD_STR(date1, n, part)
Description
Performs date arithmetic on a date string.
This calculation is specified by the arguments n
and part
.
For example a value of 3 for n
and a value of day
for part
would add 3 days to the date specified by date1
.
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing the date in a supported date format.
- n
-
The value to increment the date component by. This value must be an integer, or any valid expression which evaluates to an integer, and may be negative to perform date subtraction.
If a non-integer is passed to the function then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function then
null
is returned.
Return Value
An integer representing the result of the calculation as an Epoch/UNIX timestamp in milliseconds.
Examples
SELECT DATE_ADD_STR('2016-05-15 03:59:00Z', 3, 'day') as add_3_days,
DATE_ADD_STR('2016-05-15 03:59:00Z', 3, 'year') as add_3_years,
DATE_ADD_STR('2016-05-15 03:59:00Z', -3, 'day') as sub_3_days,
DATE_ADD_STR('2016-05-15 03:59:00Z', -3, 'year') as sub_3_years;
[
{
"add_3_days": "2016-05-18T03:59:00Z",
"add_3_years": "2019-05-15T03:59:00Z",
"sub_3_days": "2016-05-12T03:59:00Z",
"sub_3_years": "2013-05-15T03:59:00Z"
}
]
DATE_DIFF_MILLIS(date1, date2, part)
Description
Finds the elapsed time between two Epoch/UNIX timestamps.
This elapsed time is measured from the date specified by date2
to the date specified by date1
.
If date1
is greater than date2
, then the value returned will be positive, otherwise the value returned will be negative.
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - date2
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds.
This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then
null
is returned.
Return Value
An integer representing the elapsed time (based on the specified part
) between both dates.
Examples
SELECT DATE_DIFF_MILLIS(1463543940000, 1463284740000, 'day') as add_3_days,
DATE_DIFF_MILLIS(1557892740000, 1463284740000, 'year') as add_3_years,
DATE_DIFF_MILLIS(1463025540000, 1463284740000, 'day') as sub_3_days,
DATE_DIFF_MILLIS(1368590340000, 1463284740000, 'year') as sub_3_years;
[
{
"add_3_days": 3,
"add_3_years": 3,
"sub_3_days": -3,
"sub_3_years": -3
}
]
DATE_DIFF_STR(date1, date2, part)
Description
Finds the elapsed time between two dates specified as formatted strings.
This elapsed time is measured from the date specified by date2
to the date specified by date1
.
If date1
is greater than date2
then the value returned will be positive, otherwise the value returned will be negative.
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - date2
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds.
This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then
null
is returned.
Return Value
An integer representing the elapsed time (based on the specified part
) between both dates.
Examples
Find the day difference and year difference between two strings.
SELECT DATE_DIFF_STR('2016-05-18T03:59:00Z', '2016-05-15 03:59:00Z', 'day') as add_3_days,
DATE_DIFF_STR('2019-05-15T03:59:00Z', '2016-05-15 03:59:00Z', 'year') as add_3_years,
DATE_DIFF_STR('2016-05-12T03:59:00Z', '2016-05-15 03:59:00Z', 'day') as sub_3_days,
DATE_DIFF_STR('2013-05-15T03:59:00Z', '2016-05-15 03:59:00Z', 'year') as sub_3_years;
[
{
"add_3_days": 3,
"add_3_years": 3,
"sub_3_days": -3,
"sub_3_years": -3
}
]
For this example, set the query context to the inventory
scope in the travel sample dataset.
For more information, see Query Context.
To list all hotel documents that were reviewed between two dates:
SELECT name, reviews[0].date
FROM hotel
WHERE reviews[0].date BETWEEN "2013-01-01 00:00:00 +0100" AND "2014-01-01 00:00:00 +0100";
The query can also be entered as:
SELECT name, reviews[0].date
FROM hotel
WHERE reviews[0].date BETWEEN "2013-01-01 %" AND "2014-01-01 %";
[
{
"date": "2013-06-22 18:33:50 +0300",
"name": "Medway Youth Hostel"
},
{
"date": "2013-06-13 01:39:18 +0300",
"name": "Le Clos Fleuri"
},
{
"date": "2013-07-12 12:18:02 +0300",
"name": "Glasgow Grand Central"
},
{
"date": "2013-07-02 14:32:55 +0300",
"name": "The George Hotel"
},
{
"date": "2013-12-05 09:27:07 +0300",
"name": "Bacara Resort & Spa"
},
{
"date": "2013-01-10 12:48:39 +0300",
"name": "Once Brewed YHA Hostel"
},
{
"date": "2013-09-12 02:57:37 +0300",
"name": "Sandy Patch"
},
{
"date": "2013-12-18 22:36:14 +0300",
"name": "The Granary at Roch Mill"
},
{
"date": "2013-05-08 17:54:41 +0300",
"name": "Alberta House B&B"
},
// ...
]
When querying between two dates, you must specify the full date (with time and time zone) or use the wildcard character (%). |
DATE_FORMAT_STR(date1, fmt)
Description
Converts datetime strings from one supported date string format to a different supported date string format.
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format.
If this argument is not a valid date string then
null
is returned. - fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
If an incorrect format is specified then this defaults to the combined full date and time.
Examples
SELECT DATE_FORMAT_STR('2016-05-15T00:00:23+00:00', '1111-11-11') as full_to_short,
DATE_FORMAT_STR('2016-05-15', '1111-11-11T00:00:00+00:00') as short_to_full,
DATE_FORMAT_STR('01:10:05', '1111-11-11T01:01:01Z') as time_to_full;
[
{
"full_to_short": "2016-05-15",
"short_to_full": "2016-05-15T00:00:00-07:00",
"time_to_full": "0000-01-01T01:10:05-08:00"
}
]
DATE_PART_MILLIS(date1, part [, tz])
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then
null
is returned. - tz
-
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to.
Optional argument. Defaults to the system timezone if not specified. If an incorrect time zone is provided, then
null
is returned.
Examples
SELECT DATE_PART_MILLIS(1463284740000, 'day') as day_local,
DATE_PART_MILLIS(1463284740000, 'day', 'America/Tijuana') as day_pst,
DATE_PART_MILLIS(1463284740000, 'day', 'UTC') as day_utc,
DATE_PART_MILLIS(1463284740000, 'month') as month,
DATE_PART_MILLIS(1463284740000, 'week') as week,
DATE_PART_MILLIS(1463284740000, 'year') as year;
[
{
"day_local": 14,
"day_pst": 14,
"day_utc": 15,
"month": 5,
"week": 20,
"year": 2016
}
]
DATE_PART_STR(date1, part)
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then
null
is returned.
Examples
SELECT DATE_PART_STR('2016-05-15T03:59:00Z', 'day') as day,
DATE_PART_STR('2016-05-15T03:59:00Z', 'millisecond') as millisecond,
DATE_PART_STR('2016-05-15T03:59:00Z', 'month') as month,
DATE_PART_STR('2016-05-15T03:59:00Z', 'week') as week,
DATE_PART_STR('2016-05-15T03:59:00Z', 'year') as year;
[
{
"day": 15,
"millisecond": 0,
"month": 5,
"week": 20,
"year": 2016
}
]
DATE_RANGE_MILLIS(date1, date2, part [,n])
Description
Generates an array of dates from the start date specified by date1
and the end date specified by date2
, as Epoch/UNIX timestamps.
The difference between each subsequent generated date can be adjusted.
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - date2
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds.
This is the value that is subtracted from
date1
.If this argument is not an integer, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then
null
is returned. - n
-
An integer, or any valid expression which evaluates to an integer, representing the value by which to increment the part component for each generated date.
Optional argument. If not specified, this defaults to 1. If a value which is not an integer is specified, then
null
is returned.
Return Value
An array of integers representing the generated dates, as Epoch/UNIX timestamps, between date1
and date2
.
Limitations
It is possible to generate very large arrays using this function. In some cases the query engine may be unable to process all of these and cause excessive resource consumption. It is therefore recommended that you first validate the inputs to this function to ensure that the generated result is a reasonable size.
If the start date is greater than the end date passed to the function then an error will not be thrown, but the result array will be empty.
An array of descending dates can be generated by setting the start date greater than the end date and specifying a negative value for n
.
Examples
Range of milliseconds by month.
SELECT DATE_RANGE_MILLIS(1480752000000, 1475478000000, 'month', -1) as Milliseconds;
[
{
"Milliseconds": [
1480752000000,
1478156400000
]
}
]
Range of milliseconds by previous month.
SELECT DATE_RANGE_MILLIS(1480752000000, 1449129600000, 'month', -1) as Months;
[
{
"Months": [
1480752000000,
1478156400000,
1475478000000,
1472886000000,
1470207600000,
1467529200000,
1464937200000,
1462258800000,
1459666800000,
1456992000000,
1454486400000,
1451808000000
]
}
]
DATE_RANGE_STR(start_date, end_date, date_interval [, quantity_int ])
Description
Generates an array of date strings between the start date and end date, calculated by the interval and quantity values. The input dates can be in any of the supported date formats.
Arguments
- start_date
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date used as the start date of the array generation.
If this argument is not an integer, then
null
is returned. - end_date
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date used as the end date of the array generation, and this value is exclusive, that is, the end date will not be included in the result.
If this argument is not an integer, then
null
is returned. - date_interval
-
A string, or any valid expression which evaluates to a string, representing the component of the date to increment.
If an invalid part is passed to the function, then
null
is returned. - quantity_int
-
An integer, or any valid expression which evaluates to an integer, representing the value by which to increment the interval component for each generated date.
Optional argument. If not specified, this defaults to 1. If a value which is not an integer is specified, then
null
is returned.
Return Value
An array of strings representing the generated dates, as date strings, between start_date
and end_date
.
Limitations
-
It is possible to generate very large arrays using this function. In some cases the query engine may be unable to process all of these and cause excessive resource consumption. It is therefore recommended that you first validate the inputs of this function to ensure that the generated result is a reasonable size.
-
If the
start_date
is greater than theend_date
, then an error will not be thrown, but the result array will be empty. An array of descending dates can be generated by setting thestart_date
greater than theend_date
and specifying a negative value forquantity_number
. -
From 4.6.2, both specified dates can be different acceptable date formats; but prior to 4.6.2, both specified dates must have the same string format, otherwise
null
will be returned. To ensure that both dates have the same format, you should use DATE_FORMAT_STR().
Examples
Ranges by quarters.
SELECT DATE_RANGE_STR('2015-11-30T15:04:05.999', '2017-04-14T15:04:06.998', 'quarter')
AS Quarters;
[
{
"Quarters": [
"2015-11-30T15:04:05.999",
"2016-03-01T15:04:05.999",
"2016-06-01T15:04:05.999",
"2016-09-01T15:04:05.999",
"2016-12-01T15:04:05.999",
"2017-03-01T15:04:05.999"
]
}
]
Ranges by a single day.
SELECT DATE_RANGE_STR('2016-01-01T15:04:05.999', '2016-01-05T15:04:05.998', 'day', 1)
AS Days;
[
{
"Days": [
"2016-01-01T15:04:05.999",
"2016-01-02T15:04:05.999",
"2016-01-03T15:04:05.999",
"2016-01-04T15:04:05.999"
]
}
]
Ranges by four months.
SELECT DATE_RANGE_STR('2018-01-01','2019-01-01', 'month', 4)
AS Months;
[
{
"Months": [
"2018-01-01",
"2018-05-01",
"2018-09-01"
]
}
]
Ranges by previous days.
SELECT DATE_RANGE_STR('2016-01-05T15:04:05.999', '2016-01-01T15:04:06.998', 'day', -1)
AS Previous;
[
{
"Previous": [
"2016-01-05T15:04:05.999",
"2016-01-04T15:04:05.999",
"2016-01-03T15:04:05.999",
"2016-01-02T15:04:05.999"
]
}
]
Ranges by month.
SELECT DATE_RANGE_STR('2015-01-01T01:01:01', '2015-12-11T00:00:00', 'month', 1)
AS Months;
[
{
"Months": [
"2015-01-01T01:01:01",
"2015-02-01T01:01:01",
"2015-03-01T01:01:01",
"2015-04-01T01:01:01",
"2015-05-01T01:01:01",
"2015-06-01T01:01:01",
"2015-07-01T01:01:01",
"2015-08-01T01:01:01",
"2015-09-01T01:01:01",
"2015-10-01T01:01:01",
"2015-11-01T01:01:01",
"2015-12-01T01:01:01"
]
}
]
DATE_TRUNC_MILLIS(date1, part)
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date used as the date to truncate.
If this argument is not an integer, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component to truncate to. This function accepts the components
millennium
,century
,decade
,year
,quarter
,month
,week
, andiso_week
.If an invalid part is specified, then
null
is returned.
DATE_TRUNC_STR(date1, part)
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date that is truncated.
If this argument is not a valid date format, then
null
is returned. - part
-
A string, or any valid expression which evaluates to a string, representing the component to truncate to. This function accepts the components
millennium
,century
,decade
,year
,quarter
,month
,week
, andiso_week
.If an invalid part is specified, then
null
is returned.
DURATION_TO_STR(duration)
Arguments
- duration
-
A number, or any valid expression which evaluates to a number, which represents the duration to convert to a string. This value is specified in nanoseconds (1×10-9 seconds).
If a value which is not a number is specified, then
null
is returned.
MILLIS(date1)
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to Epoch/UNIX milliseconds.
If this argument is not a valid date format, then
null
is returned.
MILLIS_TO_LOCAL(date1 [, fmt])
Alias for MILLIS_TO_STR().
MILLIS_TO_STR(date1 [, fmt ])
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date to convert.
If this argument is not an integer, then
null
is returned. - fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
In some cases, where the timestamp is smaller than the duration of the provided part, this function returns the incorrect result. It is recommended that you do not use this function for very small Epoch/UNIX timestamps.
Examples
SELECT MILLIS_TO_STR(1463284740000) as full_date,
MILLIS_TO_STR(1463284740000, 'invalid format') as invalid_format,
MILLIS_TO_STR(1463284740000, '1111-11-11') as short_date;
[
{
"full_date": "2016-05-14T20:59:00-07:00",
"invalid_format": "2016-05-14T20:59:00-07:00",
"short_date": "2016-05-14"
}
]
MILLIS_TO_TZ(date1, tz [, fmt])
Description
Converts an Epoch/UNIX timestamp into the specified time zone in the specified date string format.
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date to convert.
If this argument is not an integer, then
null
is returned. - tz
-
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to. Optional argument. Defaults to the system timezone if not specified.
If an incorrect time zone is provided, then
null
is returned. - fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If no format or an incorrect format is specified, then this defaults to the combined full date and time.
MILLIS_TO_UTC(date1 [, fmt])
Arguments
- date1
-
An integer, or any valid expression which evaluates to an integer, representing a Epoch/UNIX timestamp in milliseconds. This is the date to convert to UTC.
If this argument is not an integer, then
null
is returned. - fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Examples
SELECT MILLIS_TO_UTC(1463284740000) as full_date,
MILLIS_TO_UTC(1463284740000, 'invalid format') as invalid_format,
MILLIS_TO_UTC(1463284740000, '1111-11-11') as short_date;
[
{
"full_date": "2016-05-15T03:59:00Z",
"invalid_format": "2016-05-15T03:59:00Z",
"short_date": "2016-05-15"
}
]
MILLIS_TO_ZONE_NAME(date1, tz [, fmt])
Alias for MILLIS_TO_TZ()
NOW_LOCAL([fmt])
Description
The timestamp of the query as date string in the system timezone. Will not vary during a query.
Arguments
- fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If no format or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_LOCAL() instead.
Examples
Various arguments of NOW_LOCAL().
SELECT NOW_LOCAL() as full_date,
NOW_LOCAL('invalid date') as invalid_date,
NOW_LOCAL('1111-11-11') as short_date;
[
{
"full_date": "2018-01-23T14:03:40.26-08:00",
"invalid_date": "2018-01-23T14:03:40.26-08:00",
"short_date": "2018-01-23"
}
]
Difference between NOW_LOCAL() and CLOCK_LOCAL().
SELECT NOW_LOCAL(), NOW_LOCAL(), NOW_LOCAL(), NOW_LOCAL(), NOW_LOCAL(), CLOCK_LOCAL();
[
{
"$1": "2018-01-23T14:06:20.254-08:00",
"$2": "2018-01-23T14:06:20.254-08:00",
"$3": "2018-01-23T14:06:20.254-08:00",
"$4": "2018-01-23T14:06:20.254-08:00",
"$5": "2018-01-23T14:06:20.254-08:00",
"$6": "2018-01-23T14:06:20.256-08:00"
}
]
NOW_MILLIS()
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_MILLIS() instead.
Examples
The time now in milliseconds.
SELECT NOW_MILLIS() as NowInMilliseconds;
[
{
"NowInMilliseconds": 1516745378065.12
}
]
Difference between NOW_MILLIS() and CLOCK_MILLIS().
SELECT NOW_MILLIS(), NOW_MILLIS(), NOW_MILLIS(), NOW_MILLIS(), CLOCK_MILLIS();
[
{
"$1": 1516745528579.607,
"$2": 1516745528579.607,
"$3": 1516745528579.607,
"$4": 1516745528580.29
}
]
NOW_TZ(tz [, fmt])
Description
The timestamp of the query as date string in the specified timezone. Will not vary during a query.
Arguments
- tz
-
A string, or any valid expression which evaluates to a string, representing the timezone to convert the query timestamp to.
If an incorrect time zone is provided then
null
is returned. - fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Return Value
A date string in the format specified representing the timestamp of the query in the specified timezone.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_TZ() instead.
Examples
Various arguments for NOW_TZ().
SELECT NOW_TZ('invalid tz') as invalid_tz,
NOW_TZ('Asia/Kolkata') as ist,
NOW_TZ('UTC') as utc,
NOW_TZ('UTC', '1111-11-11') as utc_short_date;
[
{
"invalid_tz": null,
"ist": "2018-01-24T03:43:36.457+05:30",
"utc": "2018-01-23T22:13:36.457Z",
"utc_short_date": "2018-01-23"
}
]
Difference between NOW_TZ() and CLOCK_TZ().
SELECT NOW_TZ('UTC'), NOW_TZ('UTC'), NOW_TZ('UTC'), CLOCK_TZ('UTC');
[
{
"$1": "2018-01-23T22:15:59.551Z",
"$2": "2018-01-23T22:15:59.551Z",
"$3": "2018-01-23T22:15:59.551Z",
"$4": "2018-01-23T22:15:59.552Z"
}
]
NOW_STR([fmt])
Description
The timestamp of the query as date string in the system timezone. Will not vary during a query.
Arguments
- fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_STR() instead.
Examples
Various arguments for NOW_STR().
SELECT NOW_STR() as full_date,
NOW_STR('invalid date') as invalid_date,
NOW_STR('1111-11-11') as short_date;
[
{
"full_date": "2018-01-23T14:16:58.075-08:00",
"invalid_date": "2018-01-23T14:16:58.075-08:00",
"short_date": "2018-01-23"
}
]
Difference between NOW_STR() and CLOCK_STR().
SELECT NOW_STR(), NOW_STR(), NOW_STR(), NOW_STR(), NOW_STR(), NOW_STR(), CLOCK_STR();
[
{
"$1": "2018-01-23T14:18:37.605-08:00",
"$2": "2018-01-23T14:18:37.605-08:00",
"$3": "2018-01-23T14:18:37.605-08:00",
"$4": "2018-01-23T14:18:37.605-08:00",
"$5": "2018-01-23T14:18:37.605-08:00",
"$6": "2018-01-23T14:18:37.605-08:00",
"$7": "2018-01-23T14:18:37.607-08:00"
}
]
NOW_UTC([fmt])
Arguments
- fmt
-
A string, or any valid expression which evaluates to a string, representing a supported date format to output the result as.
Optional argument. If unspecified or an incorrect format is specified, then this defaults to the combined full date and time.
Limitations
If this function is called multiple times within the same query it will always return the same time. If you wish to use the system time when the function is evaluated, use CLOCK_MILLIS() instead.
Examples
The current UTC time.
SELECT NOW_UTC() as CurrentUTC;
[
{
"CurrentUTC": "2018-01-23T22:20:43.971Z"
}
]
Difference between NOW_UTC() and CLOCK_UTC().
SELECT NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), NOW_UTC(), CLOCK_UTC();
[
{
"$1": "2018-01-23T22:21:46.769Z",
"$2": "2018-01-23T22:21:46.769Z",
"$3": "2018-01-23T22:21:46.769Z",
"$4": "2018-01-23T22:21:46.769Z",
"$5": "2018-01-23T22:21:46.769Z",
"$6": "2018-01-23T22:21:46.769Z",
"$7": "2018-01-23T22:21:46.769Z",
"$8": "2018-01-23T22:21:46.77Z"
}
]
STR_TO_DURATION(duration)
Description
Converts a string representation of a time duration into nanoseconds. This accepts the following units:
-
nanoseconds (
ns
) -
microseconds (
us
orµs
) -
milliseconds (
ms
) -
seconds (
s
) -
minutes (
m
) -
hours (
h
)
Arguments
- duration
-
A string, or any valid expression which evaluates to a string, representing the duration to convert.
If an invalid duration string is specified, then
null
is returned.
Examples
SELECT STR_TO_DURATION('1h') as hour,
STR_TO_DURATION('1us') as microsecond,
STR_TO_DURATION('1ms') as millisecond,
STR_TO_DURATION('1m') as minute,
STR_TO_DURATION('1ns') as nanosecond,
STR_TO_DURATION('1s') as second;
[
{
"hour": 3600000000000,
"microsecond": 1000,
"millisecond": 1000000,
"minute": 60000000000,
"nanosecond": 1,
"second": 1000000000
}
]
STR_TO_MILLIS(date1 [, format])
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing the date to convert to Epoch/UNIX milliseconds.
If this argument is not a valid date format, then
null
is returned. - format
-
A string, or any valid expression which evaluates to a string, representing the expected format of the input date string, using the Go language reference date.
Optional argument. If not specified, the input date string must be in a supported date format. If an incorrect format is provided, then
null
is returned.
STR_TO_UTC(date1)
Description
Converts a date string into the equivalent date in UTC. The output date format follows the date format of the date passed as input.
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to UTC.
If this argument is not a valid date format, then
null
is returned.
STR_TO_TZ(date1, tz)
Description
Converts a date string to its equivalent in the specified timezone. The output date format follows the date format of the date passed as input.
Arguments
- date1
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to UTC.
If this argument is not a valid date format then
null
is returned. - tz
-
A string, or any valid expression which evaluates to a string, representing the timezone to convert the local time to.
If this argument is not a valid timezone, then
null
is returned.
STR_TO_ZONE_NAME(date1, tz)
Alias for STR_TO_TZ().
WEEKDAY_MILLIS(expr [, tz ])
Description
Converts a date string to its equivalent in the specified timezone. The output date format follows the date format of the date passed as input.
Arguments
- expr
-
An integer, or any valid expression which evaluates to an integer, representing an Epoch/UNIX timestamp in milliseconds.
- tz
-
A string, or any valid expression which evaluates to a string, representing the timezone to for the expr argument.
Optional argument. Defaults to the system timezone if not specified. If an incorrect time zone is provided then
null
is returned.
WEEKDAY_STR(date)
Description
Returns the day of the week string value from the input date string. Returns the weekday name from the input date in Unix timestamp. Note that his function returns the string value of the day of the week, where DATE_PART_STR() with part = "dow" returns an integer value of the weekday (0-6).
Arguments
- date
-
A string, or any valid expression which evaluates to a string, representing a date in a supported date format. This is the date to convert to UTC.
If this argument is not a valid date format then
null
is returned.