Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc]date func #1856

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
@@ -1,6 +1,6 @@
---
{
"title": "date_ceil",
"title": "DATE_CEIL",
"language": "en"
}
---
Expand All @@ -24,83 +24,93 @@ specific language governing permissions and limitations
under the License.
-->

## date_ceil
### Description
**Syntax**
## Description

`DATETIME DATE_CEIL(DATETIME datetime, INTERVAL period type)`
`date_ceil` rounds a given date to the next upper boundary of the specified time interval.

## Syntax

Convert the date to the nearest rounding up time of the specified time interval period.
`DATE_CEIL(<datetime>, INTERVAL <period> <type>)`

The datetime parameter is a valid date expression.
## Parameters

The period parameter specifies how many units each cycle consists of, starting from 0001-01-01T00:00:00
| Parameter | Description |
| -- | -- |
| `datetime` | The argument is a valid date expression |
| `period` | The argument specifies how many units make up each period, with the start time being 0001-01-01T00:00:00 |
| `type` | The argument can be: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND|

Type: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.
## Return Value

### Example
The return value is a date or time, representing the result of rounding the input value up to the specified unit.

## Examples

```sql
select date_ceil("2023-07-13 22:28:18",interval 5 second);
```
mysql [(none)]>select date_ceil("2023-07-13 22:28:18",interval 5 second);

```text
+--------------------------------------------------------------+
| second_ceil('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+--------------------------------------------------------------+
| 2023-07-13 22:28:20 |
+--------------------------------------------------------------+
1 row in set (0.01 sec)
```

mysql [(none)]>select date_ceil("2023-07-13 22:28:18",interval 5 minute);
```sql
select date_ceil("2023-07-13 22:28:18",interval 5 minute);
+--------------------------------------------------------------+
| minute_ceil('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+--------------------------------------------------------------+
| 2023-07-13 22:30:00 |
+--------------------------------------------------------------+
1 row in set (0.01 sec)
```

```sql
select date_ceil("2023-07-13 22:28:18",interval 5 hour);
```

mysql [(none)]>select date_ceil("2023-07-13 22:28:18",interval 5 hour);
```text
+------------------------------------------------------------+
| hour_ceil('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+------------------------------------------------------------+
| 2023-07-13 23:00:00 |
+------------------------------------------------------------+
1 row in set (0.01 sec)
```

mysql [(none)]>select date_ceil("2023-07-13 22:28:18",interval 5 day);
```sql
select date_ceil("2023-07-13 22:28:18",interval 5 day);
```

```text
+-----------------------------------------------------------+
| day_ceil('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+-----------------------------------------------------------+
| 2023-07-15 00:00:00 |
+-----------------------------------------------------------+
1 row in set (0.00 sec)
```

mysql [(none)]>select date_ceil("2023-07-13 22:28:18",interval 5 month);
```sql
select date_ceil("2023-07-13 22:28:18",interval 5 month);
```

```text
+-------------------------------------------------------------+
| month_ceil('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+-------------------------------------------------------------+
| 2023-12-01 00:00:00 |
+-------------------------------------------------------------+
1 row in set (0.01 sec)
```

```sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block 之间最好加个空行,防止渲染错误

select date_ceil("2023-07-13 22:28:18",interval 5 year);
```

mysql [(none)]>select date_ceil("2023-07-13 22:28:18",interval 5 year);
```text
+------------------------------------------------------------+
| year_ceil('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+------------------------------------------------------------+
| 2026-01-01 00:00:00 |
+------------------------------------------------------------+
1 row in set (0.00 sec)
```

### Keywords

DATE_CEIL,DATE,CEIL

### Best Practices

See also
- [second_ceil](./second_ceil)
- [minute_ceil](./minute_ceil)
- [hour_ceil](./hour_ceil)
- [day_ceil](./day_ceil)
- [month_ceil](./month_ceil)
- [year_ceil](./year_ceil)
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
{
"title": "date_floor",
"title": "DATE_FLOOR",
"language": "en"
}
---
Expand All @@ -24,91 +24,108 @@ specific language governing permissions and limitations
under the License.
-->

## date_floor
### Description
**Syntax**
## Description

`DATETIME DATE_FLOOR(DATETIME datetime, INTERVAL period type)`
`date_floor` rounds a given date to the closest lower boundary of the specified time interval.

## Syntax

Converts a date to the nearest rounding down time of a specified time interval period.
`DATE_FLOOR(<datetime>, INTERVAL <period> <type>)`

The datetime parameter is a valid date expression.
## Parameters

The period parameter specifies how many units each cycle consists of, starting from 0001-01-01T00:00:00
| Parameter | Description |
| -- | -- |
| `datetime` | The argument is a valid date expression |
| `period` | The argument specifies how many units make up each period, with the start time being 0001-01-01T00:00:00 |
| `type` | The argument can be: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND|

Type: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.
## Return Value

### Example
The return value is a date or time, representing the result of rounding the input value down to the specified unit.

## Examples

```sql
select date_floor("0001-01-01 00:00:16",interval 5 second);
```
mysql>select date_floor("0001-01-01 00:00:16",interval 5 second);

```text
+---------------------------------------------------------------+
| second_floor('0001-01-01 00:00:16', 5, '0001-01-01 00:00:00') |
+---------------------------------------------------------------+
| 0001-01-01 00:00:15 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)
```

mysql>select date_floor("0001-01-01 00:00:18",interval 5 second);
```sql
select date_floor("0001-01-01 00:00:18",interval 5 second);
```

```text
+---------------------------------------------------------------+
| second_floor('0001-01-01 00:00:18', 5, '0001-01-01 00:00:00') |
+---------------------------------------------------------------+
| 0001-01-01 00:00:15 |
+---------------------------------------------------------------+
1 row in set (0.01 sec)
```

mysql>select date_floor("2023-07-13 22:28:18",interval 5 minute);
```sql
select date_floor("2023-07-13 22:28:18",interval 5 minute);
```

```text
+---------------------------------------------------------------+
| minute_floor('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+---------------------------------------------------------------+
| 2023-07-13 22:25:00 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)
```

```sql
select date_floor("2023-07-13 22:28:18",interval 5 hour);
```

mysql>select date_floor("2023-07-13 22:28:18",interval 5 hour);
```text
+-------------------------------------------------------------+
| hour_floor('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+-------------------------------------------------------------+
| 2023-07-13 18:00:00 |
+-------------------------------------------------------------+
1 row in set (0.01 sec)
```

```sql
select date_floor("2023-07-13 22:28:18",interval 5 day);
```

mysql>select date_floor("2023-07-13 22:28:18",interval 5 day);
```text
+------------------------------------------------------------+
| day_floor('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+------------------------------------------------------------+
| 2023-07-10 00:00:00 |
+------------------------------------------------------------+
1 row in set (0.00 sec)
```

```sql
select date_floor("2023-07-13 22:28:18",interval 5 month);
```

mysql>select date_floor("2023-07-13 22:28:18",interval 5 month);
```text
+--------------------------------------------------------------+
| month_floor('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+--------------------------------------------------------------+
| 2023-07-01 00:00:00 |
+--------------------------------------------------------------+
1 row in set (0.01 sec)
```

```sql
select date_floor("2023-07-13 22:28:18",interval 5 year);
```

mysql>select date_floor("2023-07-13 22:28:18",interval 5 year);
```text
+-------------------------------------------------------------+
| year_floor('2023-07-13 22:28:18', 5, '0001-01-01 00:00:00') |
+-------------------------------------------------------------+
| 2021-01-01 00:00:00 |
+-------------------------------------------------------------+

```

### Keywords

DATE_FLOOR,DATE,FLOOR

### Best Practices

See also
- [second_floor](./second_floor)
- [minute_floor](./minute_floor)
- [hour_floor](./hour_floor)
- [day_floor](./day_floor)
- [month_floor](./month_floor)
- [year_floor](./year_floor)
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,41 @@ specific language governing permissions and limitations
under the License.
-->

## extract
### description
#### Syntax
## Description

`INT extract(unit FROM DATETIME)`
The `extract` function is used to extract a specified part of a date or time value, such as the year, month, day, hour, minute, second, etc. This function is commonly used to extract specific time components from a datetime field for calculation, comparison, or display.

Extract DATETIME The value of a specified unit. The unit can be year, day, hour, minute, second or microsecond
## Syntax

### Example
`EXTRACT(<unit> FROM <datetime>)`

## Parameters

| Parameter | Description |
| -- | -- |
| `unit` | The unit to extract from the DATETIME. Possible values are year, month, day, hour, minute, second, or microsecond |
| `datetime` | The argument is a valid date expression |

## Return Value

The return value is the extracted part of the date or time (such as an integer), depending on the unit being extracted.

## Examples

```sql
select extract(year from '2022-09-22 17:01:30') as year,
extract(month from '2022-09-22 17:01:30') as month,
extract(day from '2022-09-22 17:01:30') as day,
extract(hour from '2022-09-22 17:01:30') as hour,
extract(minute from '2022-09-22 17:01:30') as minute,
extract(second from '2022-09-22 17:01:30') as second,
extract(microsecond from cast('2022-09-22 17:01:30.000123' as datetimev2(6))) as microsecond;
```
mysql> select extract(year from '2022-09-22 17:01:30') as year,
-> extract(month from '2022-09-22 17:01:30') as month,
-> extract(day from '2022-09-22 17:01:30') as day,
-> extract(hour from '2022-09-22 17:01:30') as hour,
-> extract(minute from '2022-09-22 17:01:30') as minute,
-> extract(second from '2022-09-22 17:01:30') as second,
-> extract(microsecond from cast('2022-09-22 17:01:30.000123' as datetimev2(6))) as microsecond;

```text
+------+-------+------+------+--------+--------+-------------+
| year | month | day | hour | minute | second | microsecond |
+------+-------+------+------+--------+--------+-------------+
| 2022 | 9 | 22 | 17 | 1 | 30 | 123 |
+------+-------+------+------+--------+--------+-------------+
```

### keywords

extract
Loading