SQL Date and Time Functions and Operators
NuoDB provides functional support for date and time value processing.
Date/Time Operators
Addition and subtraction operations may be performed on DATE
, TIMESTAMP
and TIMESTAMP WITHOUT TIME ZONE
values. The table below illustrates this functionality. Input may be of any of the following:
-
an expression (i.e., table column) of
DATE
,TIMESTAMP
orTIMESTAMP WITHOUT TIME ZONE
data type -
a Date/Time Function (see below)
-
an integer — valid only as the right-side operator, and returns a data type consistent with the left side operator
Operator | Description |
---|---|
+ |
Adding integer to a date/time literal
|
+ |
Adding integer to a date/time function
|
- |
Subtracting integer from a date/time literal
|
Date/Time Functions
The following table presents date and time value processing functions.
Function | Description |
---|---|
CONVERT_TZ ( |
Converts a datetime in the source time zone to the equivalent datetime in the target time zone.
|
CURRENT DATE |
Returns the date of the start of the current transaction.
|
CURRENT_DATE |
As with
|
CURRENT TIME |
Returns the time of the start of the current transaction.
|
CURRENT_TIME |
As with
|
CURRENT TIMESTAMP |
Returns the timestamp of the start of the current transaction. See
|
CURRENT_TIMESTAMP |
As with
|
DATE ( |
Converts a string to a
|
DATE_ADD ( |
Adds a temporal interval to a date/time input value.
|
DATEDIFF ( |
Returns a BIGINT value representing the time between two dates in units. The return value represents the specified boundaries crossed between the specified start_date and end_date.
|
DATEINTERVAL ( |
Returns a double precision value representing the time between two dates in units. The return value represents the specified boundaries crossed between the specified start_date and end_date including fractional component.
|
DATE_SUB( |
Subtracts temporal interval from a date/time input value.
|
EXTRACT( |
Extracts partial date and time values.
|
The current timestamp at the instant that the function is called.
|
Arithmetic operations on timestamps increment or decrement the DAY
component of the timestamp date by the value of the interval.
Operations running on TIMESTAMP
type arguments from standard time zones to daylight savings time zones, and vice versa, are adjusted for a 24 hour period, with an apparent result of a 23 or 25 hour day.
For example, a session time zone set to ‘America/New_York
’, with timestamp '2013-03-09 12:00' + 1
(add 1 day) will produce a 25 hour timestamp 2013-03-10 13:00
, as daylight saving time went into effect at 2013-03-10 02:00
.
Operations running on TIMESTAMP WITHOUT TIME ZONE
type arguments ignore the time zone daylight saving rules.
An operation using identical TIMESTAMP arguments may return different result when run using time zones with different daylight saving rules.
The same operation using TIMESTAMP WITHOUT TIME ZONE arguments will always return the same result, regardless of the time zone.
|
CONVERT_TZ
Function
The CONVERT_TZ
function returns the datetime in the target time zone equivalent to the input datetime in the source time zone.
The result type is TIMESTAMP WITHOUT TIME ZONE
.
Parameters
input_datetime
-
The
input_datetime
argument type isTIMESTAMP WITHOUT TIME ZONE
.STRING
or any other data type that can be converted toTIMESTAMP WITHOUT TIME ZONE
may also be used for theinput_datetime
argument. source_timezone
-
The
source_timezone
must be a valid time zone IDSTRING
and represents the time zone ofinput_datetime
. target_timezone
-
The
target_timezone
must be a valid time zone IDSTRING
and represents the time zone to whichinput_datetime
should be converted.
DATE_ADD
and DATE_SUB
Functions
The DATE_ADD
and DATE_SUB
functions return the result of adding or subtracting an INTERVAL
of time to/from an input value. The return type is driven by the data type of the input value.
Argument type |
Interval type |
Return type |
DATE |
DAY,MONTH,YEAR |
DATE |
DATE |
HOUR,MINUTE,SECOND |
NULL |
TIMESTAMP |
ANY |
TIMESTAMP |
TIMESTAMP WITHOUT TIME ZONE |
ANY |
TIMESTAMP WITHOUT TIME ZONE |
TIME |
DAY,MONTH,YEAR |
NULL |
TIME |
HOUR,MINUTE,SECOND |
TIME |
STRING LITERAL |
ANY |
TIMESTAMP |
Syntax
DATE_ADD(input_value, INTERVAL quantity_expr unit_of_time)
DATE_SUB(input_value, INTERVAL quantity_expr unit_of_time)
Parameters
input_value
-
A value of
DATE
,TIME
orTIMESTAMP
data type to be manipulated. If the value isNULL
, the function returns aNULL
value quantity_expr
-
Some quantity of
unit_of_time
, treated as an integer, specifying theINTERVAL
value to be added to theDATE
orTIMESTAMP
value. If thequantity_expr
has fractions it will be rounded up or down, with .5 or greater rounding up. If the value isNULL
, the function returns aNULL
value. unit_of_time
-
A keyword indicating the time units to be added to or subtracted from the
input_value
. If theinput_value
data type cannot be manipulated by theunit_of_time
specified, the function returns aNULL
value. Validunit_of_time
keywords allowed for each validinput_value
data type is as follows:Unit of Time DATE TIME TIMESTAMP TIMESTAMP WITHOUT TIME ZONE YEAR
X
X
X
MONTH
X
X
X
DAY
X
X
X
HOUR
X
X
X
MINUTE
X
X
X
SECOND
X
X
X
MICROSECOND
X
X
X
WEEK
X
X
X
Examples
SELECT DATE_ADD('2014-01-01', INTERVAL 1 DAY) FROM DUAL;
[DATE_ADD]
-------------------
2014-01-02 00:00:00
SELECT DATE_ADD(NOW(), INTERVAL 1 HOUR) FROM DUAL;
[DATE_ADD]
--------------------------
2014-01-20 16:50:24.392728
SELECT DATE_SUB('2015-01-01', INTERVAL 1 WEEK) FROM DUAL;
[DATE_SUB]
-------------------
2014-12-25 00:00:00
/* adding INTERVAL HOUR to input value of DATE data type returns NULL */
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 5 HOUR) FROM DUAL;
[DATE_ADD]
-----------
<null>
DATEDIFF
Function
The <interval> refers to the time increments to measure between two dates. For example, to determine the number of whole hours or days between a start date and end date. Valid values are second, minute, hour, day, and month.
The <start_date> refers to the start date of the time period to measure and the <end_date> refers to the end date of that period.
DATEINTERVAL
Function
The <interval> refers to the time increments to measure between two dates. For example, to determine the fractional value of hours or days between a start date and end date. Valid values are year, quarter, month, week, day, hour, minute, second, and millisecond.
The <start_date> refers to the start date of the time period to measure and the <end_date> refers to the end date of that period.
EXTRACT
Function
The EXTRACT
function retrieves subfields (i.e. YEAR
, HOUR
, etc.) from DATE
, TIME
, and TIMESTAMP
data type values. If the first argument is not a DATE, TIMESTAMP or TIME type, the argument is implicitly converted to a TIMESTAMP type. An error is returned if the conversion fails.
Parameters
field
-
An identifier or string that selects what field to extract from the source value. The
EXTRACT
function returns values of type double precision.
Valid values include:SECOND
,MINUTE
,HOUR
,DAY
,MONTH
,YEAR
,QUARTER
,ERA
,MILLISECOND
,DAYOFWEEK
,DAYOFYEAR
,WEEKOFMONTH
,WEEKOFYEAR
source
-
The
source
providing the value to be extracted from. This must be a value expression of typeDATE
,TIME
orTIMESTAMP
.
Examples
SELECT CURRENT_TIMESTAMP FROM DUAL;
CURRENT_TIMESTAMP
--------------------------
2014-01-16 15:54:28.350072
SELECT EXTRACT(HOUR FROM CURRENT_TIMESTAMP) FROM DUAL;
EXTRACT
--------
15
SELECT EXTRACT(YEAR FROM CURRENT_DATE) FROM DUAL;
EXTRACT
--------
2014
SELECT EXTRACT(HOUR FROM CURRENT_DATE) FROM DUAL;
EXTRACT
--------
<null>
SELECT EXTRACT(SECOND FROM '2015-01-01 15:54:28.350072') AS SECOND,
EXTRACT(MINUTE FROM '2015-01-01 15:54:28.350072') AS MINUTE,
EXTRACT(HOUR FROM '2015-01-01 15:54:28.350072') AS HOUR,
EXTRACT(DAY FROM '2015-01-01 15:54:28.350072') AS DAY,
EXTRACT(MONTH FROM '2015-01-01 15:54:28.350072') AS MONTH,
EXTRACT(QUARTER FROM '2015-01-01 15:54:28.350072') AS QUARTER,
EXTRACT(YEAR FROM '2015-01-01 15:54:28.350072') AS YEAR FROM DUAL;
SECOND MINUTE HOUR DAY MONTH QUARTER YEAR
------- ------- ----- ---- ------ -------- -----
28 54 15 1 1 1 2015
Additional Date/Time Functions
These functions are also supported by NuoDB and can be used instead of the EXTRACT
function.
Function | Description |
---|---|
YEAR( |
returns the year from
|
QUARTER( |
returns the quarter from
|
MONTH( |
returns month from
|
DAY( |
returns day from
|
HOUR( |
returns hour from
|
MINUTE( |
returns minute from
|
SECOND( |
returns second from
|
MILLISECOND( |
returns milliseconds from
|
ERA( |
returns era from
|
DAYOFWEEK( |
returns day of week from
|
DAYOFYEAR( |
returns day of year from
|
WEEKOFMONTH( |
returns week of month from
|
WEEKOFYEAR( |
returns week of year from
|