- Introduction
- Quick Start
- Table Design
- Data Loading
- Overview of data loading
- Load data from a local file system or a streaming data source using HTTP push
- Load data from HDFS or cloud storage
- Routine Load
- Spark Load
- Insert Into
- Change data through loading
- Transform data at loading
- Json Loading
- Synchronize data from MySQL
- Load data by using flink-connector-starrocks
- DataX Writer
- Data Export
- Using StarRocks
- Reference
- SQL Reference
- User Account Management
- Cluster Management
- ADMIN CANCEL REPAIR
- ADMIN CHECK TABLET
- ADMIN REPAIR
- ADMIN SET CONFIG
- ADMIN SET REPLICA STATUS
- ADMIN SHOW CONFIG
- ADMIN SHOW REPLICA DISTRIBUTION
- ADMIN SHOW REPLICA STATUS
- ALTER SYSTEM
- CANCEL DECOMMISSION
- CREATE FILE
- DROP FILE
- INSTALL PLUGIN
- SHOW BACKENDS
- SHOW BROKER
- SHOW FILE
- SHOW FRONTENDS
- SHOW FULL COLUMNS
- SHOW INDEX
- SHOW PLUGINS
- SHOW TABLE STATUS
- UNINSTALL PLUGIN
- DDL
- ALTER DATABASE
- ALTER TABLE
- ALTER VIEW
- BACKUP
- CANCEL BACKUP
- CANCEL RESTORE
- CREATE DATABASE
- CREATE INDEX
- CREATE MATERIALIZED VIEW
- CREATE REPOSITORY
- CREATE RESOURCE
- CREATE TABLE AS SELECT
- CREATE TABLE LIKE
- CREATE TABLE
- CREATE VIEW
- CREATE FUNCTION
- DROP DATABASE
- DROP INDEX
- DROP MATERIALIZED VIEW
- DROP REPOSITORY
- DROP RESOURCE
- DROP TABLE
- DROP VIEW
- DROP FUNCTION
- HLL
- RECOVER
- RESTORE
- SHOW RESOURCES
- SHOW FUNCTION
- TRUNCATE TABLE
- DML
- ALTER ROUTINE LOAD
- BROKER LOAD
- CANCEL LOAD
- DELETE
- EXPORT
- GROUP BY
- INSERT
- PAUSE ROUTINE LOAD
- RESUME ROUTINE LOAD
- ROUTINE LOAD
- SELECT
- SHOW ALTER
- SHOW BACKUP
- SHOW DATA
- SHOW DATABASES
- SHOW DELETE
- SHOW DYNAMIC PARTITION TABLES
- SHOW EXPORT
- SHOW LOAD
- SHOW PARTITIONS
- SHOW PROPERTY
- SHOW REPOSITORIES
- SHOW RESTORE
- SHOW ROUTINE LOAD
- SHOW ROUTINE LOAD TASK
- SHOW SNAPSHOT
- SHOW TABLES
- SHOW TABLET
- SHOW TRANSACTION
- SPARK LOAD
- STOP ROUTINE LOAD
- STREAM LOAD
- Data Types
- Auxiliary Commands
- Function Reference
- Java UDFs
- Window Function
- Date Functions
- convert_tz
- curdate
- current_timestamp
- curtime
- datediff
- date_add
- date_format
- date_sub
- date_trunc
- day
- dayname
- dayofmonth
- dayofweek
- dayofyear
- from_days
- from_unixtime
- hour
- minute
- month
- monthname
- now
- quarter
- second
- str_to_date
- timediff
- timestampadd
- timestampdiff
- to_date
- to_days
- unix_timestamp
- utc_timestamp
- weekofyear
- year
- hours_diff
- minutes_diff
- months_diff
- seconds_diff
- weeks_diff
- years_diff
- Aggregate Functions
- Geographic Functions
- String Functions
- JSON Functions
- Overview of JSON functions and operators
- JSON constructor functions
- JSON query and processing functions
- JSON operators
- Aggregate Functions
- Bitmap Functions
- Array Functions
- cast function
- hash function
- Cryptographic Functions
- Math Functions
- Utility Functions
- System variables
- Error code
- System limits
- SQL Reference
- Administration
- FAQ
- Deploy
- Data Migration
- SQL
- Other FAQs
- Benchmark
- Developers
- Contribute to StarRocks
- Code Style Guides
- Use the debuginfo file for debugging
- Development Environment
- Trace Tools
date_trunc
Description
Truncates a time value based on the specified date part, such as year, day, hour, or minute.
StarRocks also provides the year, quarter, month, week, day, and hour functions for you to extract the specified date part.
Syntax
DATETIME date_trunc(VARCHAR fmt, DATETIME|DATE datetime)
Parameters
datetime
: the time to truncate, which can be of the DATETIME or DATE type. The date and time must exist. Otherwise, NULL will be returned. For example,2021-02-29 11:12:13
does not exist as a date and NULL will be returned.fmt
: the date part, that is, to which precisiondatetime
will be truncated. The value must be a VARCHAR constant.fmt
must be set to a value listed in the following table. If the value is incorrect, an error will be returned.
Value | Description |
---|---|
second | Truncates to the second. |
minute | Truncates to the minute. The second part will be zero out. |
hour | Truncates to the hour. The minute and second parts will be zero out. |
day | Truncates to the day. The time part will be zero out. |
week | Truncates to the first date of the week that datetime falls in. The time part will be zero out. |
month | Truncates to the first date of the month that datetime falls in. The time part will be zero out. |
quarter | Truncates to the first date of the quarter that datetime falls in. The time part will be zero out. |
year | Truncates to the first date of the year that datetime falls in. The time part will be zero out. |
Return value
Returns a value of the DATETIME type.
If datetime
is of the DATE type and fmt
is set to hour
, minute
, or second
, the time part of the returned value defaults to 00:00:00
.
Examples
Example 1: Truncate the input time to the minute.
select date_trunc("minute", "2020-11-04 11:12:13");
+---------------------------------------------+
| date_trunc('minute', '2020-11-04 11:12:13') |
+---------------------------------------------+
| 2020-11-04 11:12:00 |
+---------------------------------------------+
Example 2: Truncate the input time to the hour.
select date_trunc("hour", "2020-11-04 11:12:13");
+-------------------------------------------+
| date_trunc('hour', '2020-11-04 11:12:13') |
+-------------------------------------------+
| 2020-11-04 11:00:00 |
+-------------------------------------------+
Example 3: Truncate the input time to the first day of a week.
select date_trunc("week", "2020-11-04 11:12:13");
+-------------------------------------------+
| date_trunc('week', '2020-11-04 11:12:13') |
+-------------------------------------------+
| 2020-11-02 00:00:00 |
+-------------------------------------------+
Example 4: Truncate the input time to the first day of a year.
select date_trunc("year", "2020-11-04 11:12:13");
+-------------------------------------------+
| date_trunc('year', '2020-11-04 11:12:13') |
+-------------------------------------------+
| 2020-01-01 00:00:00 |
+-------------------------------------------+
Example 5: Truncate a DATE value to the hour. 00:00:00
is returned as the time part.
select date_trunc("hour", "2020-11-04");
+----------------------------------+
| date_trunc('hour', '2020-11-04') |
+----------------------------------+
| 2020-11-04 00:00:00 |
+----------------------------------+