Skip to content

Commit

Permalink
Sql function second,second_ceil,second_floor,seconds_add,seconds_diff…
Browse files Browse the repository at this point in the history
…,seconds_sub,sec_to_time,time_to_sec
  • Loading branch information
bingquanzhao committed Jan 24, 2025
1 parent c57f7c3 commit 7d079ef
Show file tree
Hide file tree
Showing 64 changed files with 2,095 additions and 1,151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,34 @@ specific language governing permissions and limitations
under the License.
-->

## sec_to_time
### description
#### Syntax
## Description
The `SEC_TO_TIME` function converts a value in seconds into a `TIME` type, returning the result in the format `HH:MM:SS`.
The input seconds represent the time elapsed since the start of a day (`00:00:00`).

`TIME sec_to_time(INT timestamp)`

The parameter is a timestamp of type INT, and the function returns a time of type TIME.
## Syntax

### example

```
mysql >select sec_to_time(time_to_sec(cast('16:32:18' as time)));
+----------------------------------------------------+
| sec_to_time(time_to_sec(CAST('16:32:18' AS TIME))) |
+----------------------------------------------------+
| 16:32:18 |
+----------------------------------------------------+
1 row in set (0.53 sec)
```sql
SEC_TO_TIME(<seconds>)
```
## Parameters

| Parameter | Description |
|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| `<seconds>` | Required. The input number of seconds, representing the time elapsed since the start of a day (00:00:00). Supports positive or negative integers. |

### keywords
SEC_TO_TIME
## Return Value
- Returns a TIME value in the format `HH:MM:SS`, representing the time calculated from the start of a day (00:00:00).
- If <seconds> is NULL, the function returns NULL.

## Example
```sql
SELECT SEC_TO_TIME(59738);
```
```text
+--------------------+
| sec_to_time(59738) |
+--------------------+
| 16:35:38 |
+--------------------+
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
{
"title": "second_ceil",
"title": "SECOND_CEIL",
"language": "en"
}
---
Expand All @@ -24,39 +24,69 @@ specific language governing permissions and limitations
under the License.
-->

## second_ceil
### description
#### Syntax
## Description
The function aligns the input datetime value upwards to the nearest second boundary based on the specified period and returns the aligned datetime value.

## Syntax

```sql
DATETIME SECOND_CEIL(DATETIME datetime)
DATETIME SECOND_CEIL(DATETIME datetime, DATETIME origin)
DATETIME SECOND_CEIL(DATETIME datetime, INT period)
DATETIME SECOND_CEIL(DATETIME datetime, INT period, DATETIME origin)
SECOND_CEIL(<datetime>[, <period>][, <origin_datetime>])
```
## Parameters

Convert the date to the nearest rounding up time of the specified time interval period.

- datetime: a valid date expression.
- period: specifies how many seconds each cycle consists of.
- origin: starting from 0001-01-01T00:00:00.
| Parameter | Description |
|---------------------|---------------------------------------------------------------------------------------------------------------------------|
| `<datetime>` | Required. The input datetime value. Supports the DATETIME type. |
| `<period>` | Optional. Specifies the number of seconds in each period. Supports positive integers (INT). Defaults to 1 second. |
| `<origin_datetime>` | Optional. The starting point for alignment. Supports the DATETIME type. Defaults to 0001-01-01T00:00:00 if not specified. |

### example
## Return Value
- Returns a datetime value representing the input datetime aligned upwards to the nearest specified second boundary.
- If `<datetime>` is NULL, the function returns NULL.
- If `<datetime>` is an invalid date (e.g., 0000-00-00T00:00:00), the function returns NULL.

## Example
Only specifying `<datetime>`
```sql
SELECT SECOND_CEIL('2025-01-23 12:34:56');
```
```text
+-----------------------------------------------------------+
| second_ceil(cast('2025-01-23 12:34:56' as DATETIMEV2(0))) |
+-----------------------------------------------------------+
| 2025-01-23 12:34:56 |
+-----------------------------------------------------------+
```
mysql> select second_ceil("2023-07-13 22:28:18", 5);
Specifying `<datetime>` and `<origin_datetime>`
```sql
SELECT SECOND_CEIL('2025-01-23 12:34:56', '2025-01-01 00:00:00');
```
```text
+---------------------------------------------------------------------------------------------------------+
| second_ceil(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), cast('2025-01-01 00:00:00' as DATETIMEV2(0))) |
+---------------------------------------------------------------------------------------------------------+
| 2025-01-23 12:34:56 |
+---------------------------------------------------------------------------------------------------------+
```
Specifying `<datetime>` and `<period>`
```sql
SELECT SECOND_CEIL('2025-01-23 12:34:56', 5)
```
```text
+--------------------------------------------------------------+
| second_ceil(cast('2023-07-13 22:28:18' as DATETIMEV2(0)), 5) |
| second_ceil(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), 5) |
+--------------------------------------------------------------+
| 2023-07-13 22:28:20 |
| 2025-01-23 12:35:00 |
+--------------------------------------------------------------+
1 row in set (0.01 sec)
```

### keywords

SECOND_CEIL, SECOND, CEIL

### Best Practice

See also [date_ceil](./date_ceil)
Specifying `<datetime>``<period>` and `<origin_datetime>`
```sql
SELECT SECOND_CEIL('2025-01-23 12:34:56', 10, '2025-01-23 12:00:00');
```
```text
+-------------------------------------------------------------------------------------------------------------+
| second_ceil(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), 10, cast('2025-01-23 12:00:00' as DATETIMEV2(0))) |
+-------------------------------------------------------------------------------------------------------------+
| 2025-01-23 12:35:00 |
+-------------------------------------------------------------------------------------------------------------+
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
{
"title": "second_floor",
"title": "SECOND_FLOOR",
"language": "en"
}
---
Expand All @@ -24,39 +24,69 @@ specific language governing permissions and limitations
under the License.
-->

## second_floor
### description
#### Syntax
## Description
The function aligns the input datetime value upwards to the nearest second boundary based on the specified period and returns the aligned datetime value.

## Syntax

```sql
DATETIME SECOND_FLOOR(DATETIME datetime)
DATETIME SECOND_FLOOR(DATETIME datetime, DATETIME origin)
DATETIME SECOND_FLOOR(DATETIME datetime, INT period)
DATETIME SECOND_FLOOR(DATETIME datetime, INT period, DATETIME origin)
SECOND_FLOOR(<datetime>[, <period>][, <origin_datetime>])
```
## Parameters

Convert the date to the nearest rounding down time of the specified time interval period.

- datetime: a valid date expression.
- period: specifies how many seconds each cycle consists of.
- origin: starting from 0001-01-01T00:00:00.
| Parameter | Description |
|---------------------|---------------------------------------------------------------------------------------------------------------------------|
| `<datetime>` | Required. The input datetime value. Supports the DATETIME type. |
| `<period>` | Optional. Specifies the number of seconds in each period. Supports positive integers (INT). Defaults to 1 second. |
| `<origin_datetime>` | Optional. The starting point for alignment. Supports the DATETIME type. Defaults to 0001-01-01T00:00:00 if not specified. |

### example
## Return Value
- Returns a datetime value representing the input datetime aligned upwards to the nearest specified second boundary.
- If `<datetime>` is NULL, the function returns NULL.
- If `<datetime>` is an invalid date (e.g., 0000-00-00T00:00:00), the function returns NULL.

## Example
Only specifying `<datetime>`
```sql
SELECT SECOND_FLOOR('2025-01-23 12:34:56');
```
```text
+------------------------------------------------------------+
| second_floor(cast('2025-01-23 12:34:56' as DATETIMEV2(0))) |
+------------------------------------------------------------+
| 2025-01-23 12:34:56 |
+------------------------------------------------------------+
```
mysql> select second_floor("2023-07-13 22:28:18", 5);
Specifying `<datetime>` and `<origin_datetime>`
```sql
SELECT SECOND_FLOOR('2025-01-23 12:34:56', '2025-01-01 00:00:00');
```
```text
+----------------------------------------------------------------------------------------------------------+
| second_floor(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), cast('2025-01-01 00:00:00' as DATETIMEV2(0))) |
+----------------------------------------------------------------------------------------------------------+
| 2025-01-23 12:34:56 |
+----------------------------------------------------------------------------------------------------------+
```
Specifying `<datetime>` and `<period>`
```sql
SELECT SECOND_FLOOR('2025-01-23 12:34:56', 5)
```
```text
+---------------------------------------------------------------+
| second_floor(cast('2023-07-13 22:28:18' as DATETIMEV2(0)), 5) |
| second_floor(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), 5) |
+---------------------------------------------------------------+
| 2023-07-13 22:28:15 |
| 2025-01-23 12:34:55 |
+---------------------------------------------------------------+
1 row in set (0.10 sec)
```

### keywords

SECOND_FLOOR, SECOND, FLOOR

### Best Practice

See also [date_floor](./date_floor)
Specifying `<datetime>``<period>` and `<origin_datetime>`
```sql
SELECT SECOND_FLOOR('2025-01-23 12:34:56', 10, '2025-01-23 12:00:00');
```
```text
+--------------------------------------------------------------------------------------------------------------+
| second_floor(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), 10, cast('2025-01-23 12:00:00' as DATETIMEV2(0))) |
+--------------------------------------------------------------------------------------------------------------+
| 2025-01-23 12:34:50 |
+--------------------------------------------------------------------------------------------------------------+
```
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,32 @@ under the License.
-->

## Description
The function returns the second part of the specified datetime value. The range of seconds is 0 to 59.

Returns second information in the time type, ranging from 0,59
## Syntax

```sql
SECOND(<datetime>)
```
## Parameters

The parameter is Date or Datetime or Time type
| Parameter | Description |
|--------------|--------------------------------------------------------------------|
| `<datetime>` | The input date or datetime value. Supports DATE or DATETIME types. |

## Syntax
`INT SECOND(DATETIME date)`
## Return Value
- Returns an integer representing the second part of the input datetime value, ranging from 0 to 59.
- If the input is NULL, the function returns NULL.
- If the input is an invalid date (e.g., 0000-00-00 00:00:00), the function returns NULL.

## Example

```sql
mysql> select second('2018-12-31 23:59:59');
+-----------------------------+
| second('2018-12-31 23:59:59') |
+-----------------------------+
| 59 |
+-----------------------------+
select second('2018-12-31 23:59:59');
```
## Keywords
SECOND
```text
+---------------------------------------------+
| second(cast('2018-12-30' as DATETIMEV2(0))) |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
{
"title": "SECONDS_ADD",
"language": "en"
"title": "SECONDS_ADD",
"language": "en"
}
---

Expand All @@ -24,27 +24,38 @@ specific language governing permissions and limitations
under the License.
-->

## seconds_add
### description
#### Syntax
## Description

`DATETIME SECONDS_ADD(DATETIME date, INT seconds)`
The function adds or subtracts a specified number of seconds to/from a given datetime value and returns the resulting
datetime.

ADD a specified number of seconds from a datetime or date
## Syntax

The parameter date can be DATETIME or DATE, and the return type is DATETIME.
```sql
SECONDS_ADD(<datetime>, <seconds>)
```

### example
## Parameters

```
mysql> select seconds_add("2020-02-02 02:02:02", 1);
+---------------------------------------+
| seconds_add('2020-02-02 02:02:02', 1) |
+---------------------------------------+
| 2020-02-02 02:02:03 |
+---------------------------------------+
```
| Parameter | Description |
|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| `<datetime>` | Required. The input datetime value. Supports the DATETIME and DATE type. |
| `<seconds>` | Required. The number of seconds to add or subtract. Supports integers (INT). Positive numbers add seconds, while negative numbers subtract seconds. |

## Return Value
- Returns a datetime value of the same type as the input <datetime>.
- If `<datetime>` is NULL, the function returns NULL.
- If `<datetime>` is an invalid date (e.g., 0000-00-00T00:00:00), the function returns NULL.

### keywords
## Examples

SECONDS_ADD
```
SELECT SECONDS_ADD('2025-01-23 12:34:56', 30),SECONDS_ADD('2025-01-23 12:34:56', -30);
```
```text
+---------------------------------------------------------------+----------------------------------------------------------------+
| seconds_add(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), 30) | seconds_add(cast('2025-01-23 12:34:56' as DATETIMEV2(0)), -30) |
+---------------------------------------------------------------+----------------------------------------------------------------+
| 2025-01-23 12:35:26 | 2025-01-23 12:34:26 |
+---------------------------------------------------------------+----------------------------------------------------------------+
```
Loading

0 comments on commit 7d079ef

Please sign in to comment.