| --- |
| { |
| "title": "DATE_TRUNC", |
| "language": "en" |
| } |
| --- |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| --> |
| |
| ## date_trunc |
| |
| date_trunc |
| |
| ### Description |
| #### Syntax |
| |
| `DATETIME DATE_TRUNC(DATETIME datetime, VARCHAR unit)` |
| |
| |
| Truncates datetime in the specified time unit. |
| |
| datetime is a legal date expression. |
| |
| unit is the time unit you want to truncate. The optional values are as follows: [`second`,`minute`,`hour`,`day`,`week`,`month`,`quarter`,`year`]. |
| |
| ### example |
| |
| ``` |
| mysql> select date_trunc('2010-12-02 19:28:30', 'second'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'second') | |
| +-------------------------------------------------+ |
| | 2010-12-02 19:28:30 | |
| +-------------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'minute'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'minute') | |
| +-------------------------------------------------+ |
| | 2010-12-02 19:28:00 | |
| +-------------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'hour'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'hour') | |
| +-------------------------------------------------+ |
| | 2010-12-02 19:00:00 | |
| +-------------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'day'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'day') | |
| +-------------------------------------------------+ |
| | 2010-12-02 00:00:00 | |
| +-------------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'week'); |
| +-------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'week') | |
| +-------------------------------------------+ |
| | 2010-11-29 00:00:00 | |
| +-------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'month'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'month') | |
| +-------------------------------------------------+ |
| | 2010-12-01 00:00:00 | |
| +-------------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'quarter'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'quarter') | |
| +-------------------------------------------------+ |
| | 2010-10-01 00:00:00 | |
| +-------------------------------------------------+ |
| |
| mysql> select date_trunc('2010-12-02 19:28:30', 'year'); |
| +-------------------------------------------------+ |
| | date_trunc('2010-12-02 19:28:30', 'year') | |
| +-------------------------------------------------+ |
| | 2010-01-01 00:00:00 | |
| +-------------------------------------------------+ |
| ``` |
| ### keywords |
| |
| DATE_TRUNC,DATE,DATETIME |