Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ ADD_TIME(`<date_or_time_expr>`, `<time>`)

| Parameter | Description |
| ---------------------| ----------- |
| `<date_or_time_expr>`| A valid date expression. Supports input of datetime/date/time types. If the type is date, it will be converted to the start time of the day (00:00:00). For specific datetime/time formats, see [datetime conversion](../../../../../docs/sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [time conversion](../../../../../docs/sql-manual/basic-element/sql-data-types/conversion/time-conversion). |
| `<date_or_time_expr>`| A valid date expression. Supports input of timestamptz/datetime/date/time types. If the type is date, it will be converted to the start time of the day (00:00:00). For specific formats, see [timestamptz conversion](../../../../../current/sql-manual/basic-element/sql-data-types/conversion/timestamptz-conversion), [datetime conversion](../../../../../docs/sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [time conversion](../../../../../docs/sql-manual/basic-element/sql-data-types/conversion/time-conversion). |
| `<time>` | A valid time expression, representing the time value to be added to `<date_or_time_expr>`. If negative, it means subtraction. Supports input of time type. |

## Return Value

Returns the result of adding `<time>` to `<date_or_time_expr>`. The return type depends on the type of the first parameter:
- If the first parameter is of timestamptz type, returns timestamptz type.
- If the first parameter is of datetime type, returns datetime type.
- If the first parameter is of time type, returns time type.

Special cases:
- If any input parameter is null, returns null.
- If the first parameter is of time type and the result exceeds the time type range, returns the maximum (or minimum) time value.
- If the first parameter is of datetime type and the result exceeds the datetime type range, an error is thrown.
- If the first parameter is of datetime or timestamptz type and the result exceeds the datetime type range, an error is thrown.

## Examples

Expand All @@ -52,6 +53,14 @@ SELECT ADD_TIME(cast('12:15:20' as time), '00:10:40');
+------------------------------------------------+
| 12:26:00 |
+------------------------------------------------+

-- SET time_zone = '+08:00';
SELECT ADD_TIME('2025-10-10 11:22:33.1234567+03:00', '01:02:03');
+-----------------------------------------------------------+
| ADD_TIME('2025-10-10 11:22:33.1234567+03:00', '01:02:03') |
+-----------------------------------------------------------+
| 2025-10-10 17:24:36.123457+08:00 |
+-----------------------------------------------------------+

-- NULL parameter test
SELECT ADD_TIME(NULL, '01:00:00');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

The DATE_ADD function is used to add a specified time interval to a specified date or time value and return the calculated result.

- Supported input date types include DATE, DATETIME (such as '2023-12-31', '2023-12-31 23:59:59').
- Supported input date types include DATE, DATETIME, TIMESTAMPTZ (such as '2023-12-31', '2023-12-31 23:59:59', '2023-12-31 23:59:59+08:00').
- The time interval is specified by both a numeric value (`expr`) and a unit (`time_unit`). When `expr` is positive, it means "add", and when it is negative, it is equivalent to "subtract" the corresponding interval.

## Aliases
Expand All @@ -28,7 +28,7 @@ DATE_ADD(<date_or_time_expr>, <expr> <time_unit>)

| Parameter | Description |
| -- | -- |
| `<date_or_time_expr>` | The date/time value to be processed. Supported types: datetime or date type, with a maximum precision of six decimal places for seconds (e.g., 2022-12-28 23:59:59.999999). For specific datetime and date formats, please refer to [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<date_or_time_expr>` | The date/time value to be processed. Supported types: datetime or date type, with a maximum precision of six decimal places for seconds (e.g., 2022-12-28 23:59:59.999999). For specific formats, please refer to [timestamptz conversion](../../../../../current/sql-manual/basic-element/sql-data-types/conversion/timestamptz-conversion), [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<expr>` | The time interval to be added, of `INT` type |
| `<time_unit>` | Enumeration values: YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, DAY_SECOND, DAY_HOUR, MINUTE_SECOND, SECOND_MICROSECOND |

Expand All @@ -37,6 +37,7 @@ DATE_ADD(<date_or_time_expr>, <expr> <time_unit>)
Returns a result with the same type as <date_or_time_expr>:
- When DATE type is input, returns DATE (date part only);
- When DATETIME type input, returns DATETIME (including date and time);
- WHEN TIMESTAMPTZ type input, returns TIMESTAMPTZ (including date, time, and timezone offset).
- Input with scale (such as '2024-01-01 12:00:00.123') will preserve the scale, with a maximum of six decimal places.

Special cases:
Expand Down Expand Up @@ -129,6 +130,14 @@ mysql> select date_add("2025-10-10 10:10:10.123456", INTERVAL "1.1" SECOND_MICR
| 2025-10-10 10:10:11.223456 |
+---------------------------------------------------------------------------+

-- Example of TimestampTz type, SET time_zone = '+08:00'
select DATE_ADD('2023-01-01 23:22:33+03:00', INTERVAL 1 DAY);
+-------------------------------------------------------+
| DATE_ADD('2023-01-01 23:22:33+03:00', INTERVAL 1 DAY) |
+-------------------------------------------------------+
| 2023-01-03 04:22:33+08:00 |
+-------------------------------------------------------+

-- Illegal unit
select DATE_ADD('2023-12-31 23:00:00', INTERVAL 2 sa);
ERROR 1105 (HY000): errCode = 2, detailMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Description

The DATE_SUB function is used to subtract a specified time interval from a given date or time value and return the calculated date or time result. It supports operations on DATE (date only) and DATETIME (date and time) types, where the time interval is defined by both a numerical value and a unit.
The DATE_SUB function is used to subtract a specified time interval from a given date or time value and return the calculated date or time result. It supports operations on DATE (date only), DATETIME (date and time) and TIMESTAMPTZ(date, time, and timezone offset) types, where the time interval is defined by both a numerical value and a unit.

This function behaves generally consistently with the [date_sub function](https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_date-sub) in MySQL, but the difference is that MySQL supports compound unit additions and subtractions, such as:

Expand All @@ -33,7 +33,7 @@ DATE_SUB(<date_or_time_part>, <expr> <time_unit>)

| Parameter | Description |
| -- | -- |
| `<date_or_time_part>` | A valid date value, supporting datetime or date type. For specific datetime and date formats, please refer to [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<date_or_time_part>` | A valid date value, supporting datetime or date type. For specificformats, please refer to [timestamptz conversion](../../../../../current/sql-manual/basic-element/sql-data-types/conversion/timestamptz-conversion), [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<expr>` | The time interval to be subtracted, of type `INT` |
| `<time_unit>` | Enumerated values: YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND |

Expand All @@ -42,6 +42,7 @@ DATE_SUB(<date_or_time_part>, <expr> <time_unit>)
Returns a calculated result with the same type as date:
- When input is DATE, returns DATE (date part only);
- When input is DATETIME, returns DATETIME (including date and time).
- When input is TIMESTAMPTZ, returns TIMESTAMPTZ (including date, time, and timezone offset).
- For datetime types with scale, the scale will be preserved and returned.

Special cases:
Expand Down Expand Up @@ -101,6 +102,14 @@ mysql> select date_sub('2023-12-31 23:59:59', INTERVAL 61 QUARTER);
| 2008-09-30 23:59:59 |
+------------------------------------------------------+

-- Example of TimeStampTz type, SET time_zone = '+08:00'
SELECT DATE_SUB('2024-02-05 02:03:04.123+12:00', INTERVAL 1 DAY);
+-----------------------------------------------------------+
| DATE_SUB('2024-02-05 02:03:04.123+12:00', INTERVAL 1 DAY) |
+-----------------------------------------------------------+
| 2024-02-03 22:03:04.123+08:00 |
+-----------------------------------------------------------+

-- Any parameter is NULL
mysql> select date_sub('2023-01-01', INTERVAL NULL DAY);
+-------------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Description

The HOURS_ADD function adds or subtracts a specified number of hours to/from an input date or datetime value and returns the calculated new datetime. This function supports both DATE and DATETIME input types. If the input is DATE type (containing only year, month, day), it defaults the time part to 00:00:00 before adding hours.
The HOURS_ADD function adds or subtracts a specified number of hours to/from an input date or datetime value and returns the calculated new datetime. This function supports both DATE, DATETIME and TIMESTAMPTZ input types. If the input is DATE type (containing only year, month, day), it defaults the time part to 00:00:00 before adding hours.

This function is consistent with the [date_add function](./date-add) and [date_add function](https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_date-add) in MySQL when using the `HOUR` unit.

Expand All @@ -22,13 +22,16 @@ HOURS_ADD(`<date_or_time_expr>`, `<hours>`)

| Parameter | Description |
| ---- | ---- |
| `<date_or_time_expr>` | A valid date expression that supports date/datetime types. For specific datetime and date formats, please refer to [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<date_or_time_expr>` | A valid date expression that supports date/datetime/timestamptz types. For specific formats, please refer to [timestamptz conversion](../../../../../current/sql-manual/basic-element/sql-data-types/conversion/timestamptz-conversion), [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<hours>` | The number of hours to add, of integer (INT) type. Can be positive or negative: positive number adds the specified hours, negative number subtracts the specified hours (equivalent to subtracting hours) |

## Return Value

Returns DATETIME type, representing the time value after adding or subtracting the specified number of hours from the input datetime.
Return the base time `<date_or_time_expr>` plus the specified hours `<hours>`, the return value type is determined by the type of the first parameter:
- If the type of the first parameter is DATE/DATETIME, then the return type is DATETIME.
- If the type of the first parameter is TIMESTAMPTZ, then the return type is TIMESTAMPTZ.

Special cases:
- If the calculation result exceeds the valid range of DATETIME type [0000-01-01 00:00:01, 9999-12-31 23:59:59], returns an error.
- Return NULL if any parameters is NULL.

Expand Down Expand Up @@ -60,6 +63,14 @@ select hours_add('2023-10-01 10:00:00', -3) ;
| 2023-10-01 07:00:00 |
+--------------------------------------+

-- Example of TimeStampTz type, SET time_zone = '+08:00'
SELECT HOURS_ADD('2025-10-10 11:22:33.123+07:00', 1);
+-----------------------------------------------+
| HOURS_ADD('2025-10-10 11:22:33.123+07:00', 1) |
+-----------------------------------------------+
| 2025-10-10 13:22:33.123+08:00 |
+-----------------------------------------------+

-- Input parameter is NULL, return NULL
select hours_add(null, 5) ;
+--------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Description

The HOURS_SUB function subtracts a specified number of hours from an input date or datetime value and returns the calculated new datetime. This function supports both DATE and DATETIME input types. If the input is DATE type (containing only year, month, day), it defaults the time part to 00:00:00.
The HOURS_SUB function subtracts a specified number of hours from an input date or datetime value and returns the calculated new datetime. This function supports both DATE, DATETIME and TIMESTAMPTZ input types. If the input is DATE type (containing only year, month, day), it defaults the time part to 00:00:00.

This function is consistent with the [date_sub function](./date-sub) and [date_sub function](https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_date-sub) in MySQL when using the `HOUR` unit.

Expand All @@ -22,13 +22,16 @@ HOURS_SUB(`<date_or_time_expr>`, `<hours>`)

| Parameter | Description |
| ---- | ---- |
| `<date_or_time_expr>` | A valid date expression that supports date/datetime types. For specific datetime and date formats, please refer to [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<date_or_time_expr>` | A valid date expression that supports date/datetime/timestamptz types. For specific formats, please refer to [timestamptz conversion](../../../../../current/sql-manual/basic-element/sql-data-types/conversion/timestamptz-conversion), [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) and [date conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/date-conversion) |
| `<hours>` | The number of hours to subtract, of INT type |

## Return Value

Returns a DATETIME type value representing the datetime after adding or subtracting the specified hours (format: YYYY-MM-DD HH:MM:SS).
Return the base time `<date_or_time_expr>` minus the specified hours `<hours>`, the return value type is determined by the type of the first parameter:
- If the type of the first parameter is DATE/DATETIME, then the return type is DATETIME.
- If the type of the first parameter is TIMESTAMPTZ, then the return type is TIMESTAMPTZ

Special cases:
- If the calculation result exceeds the valid range of DATETIME type (0000-01-01 00:00:00 to 9999-12-31 23:59:59), returns an error.
- If any input parameter is NULL, returns NULL.
- If input hours is negative, returns the datetime plus the corresponding number of hours.
Expand Down Expand Up @@ -61,6 +64,14 @@ select hours_sub('2023-10-01 10:00:00', -3);
| 2023-10-01 13:00:00 |
+--------------------------------------+

-- Example of TimeStampTz type, SET time_zone = '+08:00'
SELECT HOURS_SUB('2025-10-10 11:22:33.123+07:00', 1);
+-----------------------------------------------+
| HOURS_SUB('2025-10-10 11:22:33.123+07:00', 1) |
+-----------------------------------------------+
| 2025-10-10 11:22:33.123+08:00 |
+-----------------------------------------------+

-- Any parameter is NULL, return NULL
select hours_sub('2023-10-01 10:00:00', NULL);
+----------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@

## Description

The MICROSECONDS_ADD function adds a specified number of microseconds to the input datetime value and returns the resulting new datetime value. This function supports processing DATETIME types with microsecond precision.
The MICROSECONDS_ADD function adds a specified number of microseconds to the input datetime value and returns the resulting new datetime value. This function supports processing DATETIME or TIMESTAMPTZ types with microsecond precision.

This function behaves the same as MySQL’s [date_add function](https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_date-add) when using MICROSECOND as the unit.

## Syntax

```sql
MICROSECONDS_ADD(`<datetime>`, `<delta>`)
MICROSECONDS_ADD(`<datetime_like_type>`, `<delta>`)
```

## Parameters

| Parameter | Description |
| --------- | ----------- |
| `<datetime>` | The input datetime value, of type DATETIME. For datetime formats, see [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) |
| `<datetime_like_type>` | The input datetime value, of type DATETIME or TIMESTAMPTZ. For formats, see [timestamptz conversion](../../../../../current/sql-manual/basic-element/sql-data-types/conversion/timestamptz-conversion), [datetime conversion](../../../../sql-manual/basic-element/sql-data-types/conversion/datetime-conversion) |
| `<delta>` | The number of microseconds to add, of type BIGINT. 1 second = 1,000,000 microseconds. |

## Return Value

Returns a DATETIME type value, representing the result of adding the specified microseconds to the base time (formatted as YYYY-MM-DD HH:MM:SS.ffffff, where the fractional part precision matches the datetime precision).
Return the result of adding the specified microseconds `<delta>` to the base time `<datetime_like_type>`, with the return type being the same as the type of the first parameter.
- If the first parameter is TIMESTAMPTZ, then return TIMESTAMPTZ.
- If the first parameter is DATETIME, then return DATETIME.

- If `<delta>` is negative, the function subtracts the corresponding microseconds from the base time (i.e., MICROSECONDS_ADD(basetime, -n) is equivalent to MICROSECONDS_SUB(basetime, n)).
- If the calculation result exceeds the valid range of the DATETIME type (0000-01-01 00:00:00 to 9999-12-31 23:59:59.999999), an exception is thrown.
Expand Down Expand Up @@ -61,6 +63,14 @@ SELECT MICROSECONDS_ADD('2023-10-01', -300000);
| 2023-09-30 23:59:59.700000 |
+-----------------------------------------+

-- Example of TimeStampTz type, SET time_zone = '+08:00'
SELECT MICROSECONDS_ADD('2024-12-25 12:34:56.123+04:00', '765800');
+-------------------------------------------------------------+
| MICROSECONDS_ADD('2024-12-25 12:34:56.123+04:00', '765800') |
+-------------------------------------------------------------+
| 2024-12-25 16:34:56.888800+08:00 |
+-------------------------------------------------------------+

-- Calculation result exceeds datetime range, throws error
SELECT MICROSECONDS_ADD('9999-12-31 23:59:59.999999', 2000000) AS after_add;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation microseconds_add of 9999-12-31 23:59:59.999999, 2000000 out of range
Expand Down
Loading