date_trunc cuts off every part of a date below a given precision. This post walks through all supported type values for the China-domestically developed GBase 8a MPP database from GBASE, with the original output of each example.
Reference
The companion note on the date_part and extract date functions.
Syntax
date_trunc(type,date)
-
type: the precision position to truncate at; everything below it is cut off. -
date: normally a datetime; a date value is converted to datetime automatically. -
typeaccepts both the quoted and unquoted forms, for exampleyearand'year'. The examples below do not repeat both spelling variants.
year
The first day of the year, with everything below it set to 01-01 00:00:00:
gbase> select date_trunc(year,'2023-04-21 11:12:13.56789');
+----------------------------------------------+
| date_trunc(year,'2023-04-21 11:12:13.56789') |
+----------------------------------------------+
| 2023-01-01 00:00:00 |
+----------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
gbase> select date_trunc('year','2023-04-21 11:12:13.56789');
+------------------------------------------------+
| date_trunc('year','2023-04-21 11:12:13.56789') |
+------------------------------------------------+
| 2023-01-01 00:00:00 |
+------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
quarter
The first day of the quarter the date falls in, time part 00:00:00:
gbase> select date_trunc(quarter,'2023-04-21 11:12:13.56789');
+-------------------------------------------------+
| date_trunc(quarter,'2023-04-21 11:12:13.56789') |
+-------------------------------------------------+
| 2023-04-01 00:00:00 |
+-------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
month
The first day of the month, time part 00:00:00:
gbase> select date_trunc(month,'2023-04-21 11:12:13.56789');
+-----------------------------------------------+
| date_trunc(month,'2023-04-21 11:12:13.56789') |
+-----------------------------------------------+
| 2023-04-01 00:00:00 |
+-----------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
week
The Monday of the week the date falls in, time part 00:00:00. The Monday of a year's first week may land in the previous year:
gbase> select date_trunc(week,'2023-04-21 11:12:13.56789');
+----------------------------------------------+
| date_trunc(week,'2023-04-21 11:12:13.56789') |
+----------------------------------------------+
| 2023-04-17 00:00:00 |
+----------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
gbase> select date_trunc(week,'2023-01-1 11:12:13.56789');
+---------------------------------------------+
| date_trunc(week,'2023-01-1 11:12:13.56789') |
+---------------------------------------------+
| 2022-12-26 00:00:00 |
+---------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
day
The time part is set to 00:00:00:
gbase> select date_trunc(day,'2023-04-21 11:12:13.56789');
+---------------------------------------------+
| date_trunc(day,'2023-04-21 11:12:13.56789') |
+---------------------------------------------+
| 2023-04-21 00:00:00 |
+---------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
hour
Everything below the hour is set to 00:00:
gbase> select date_trunc(hour,'2023-04-21 11:12:13.56789');
+----------------------------------------------+
| date_trunc(hour,'2023-04-21 11:12:13.56789') |
+----------------------------------------------+
| 2023-04-21 11:00:00 |
+----------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
minute
Everything below the minute is set to 00:
gbase> select date_trunc(minute,'2023-04-21 11:12:13.56789');
+------------------------------------------------+
| date_trunc(minute,'2023-04-21 11:12:13.56789') |
+------------------------------------------------+
| 2023-04-21 11:12:00 |
+------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
gbase>
second
Everything below the second is set to 0:
gbase> select date_trunc(second,'2023-04-21 11:12:13.56789');
+------------------------------------------------+
| date_trunc(second,'2023-04-21 11:12:13.56789') |
+------------------------------------------------+
| 2023-04-21 11:12:13 |
+------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
century
The first day of the century the date falls in:
gbase> select date_trunc(century,'2023-04-21 11:12:13.56789');
+-------------------------------------------------+
| date_trunc(century,'2023-04-21 11:12:13.56789') |
+-------------------------------------------------+
| 2001-01-01 00:00:00 |
+-------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
decade
The first day of the 10-year period the date falls in, time part zero:
gbase> select date_trunc(decade,'2023-04-21 11:12:13.56789');
+------------------------------------------------+
| date_trunc(decade,'2023-04-21 11:12:13.56789') |
+------------------------------------------------+
| 2020-01-01 00:00:00 |
+------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
millennium
The first day of the millennium the date falls in:
gbase> select date_trunc(millennium,'2023-04-23 11:12:13.56789');
+----------------------------------------------------+
| date_trunc(millennium,'2023-04-23 11:12:13.56789') |
+----------------------------------------------------+
| 2001-01-01 00:00:00 |
+----------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
milliseconds
Everything below milliseconds is set to 0:
gbase> select date_trunc(milliseconds,'2023-04-23 11:12:13.56789');
+------------------------------------------------------+
| date_trunc(milliseconds,'2023-04-23 11:12:13.56789') |
+------------------------------------------------------+
| 2023-04-23 11:12:13.567000 |
+------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
microseconds
Microseconds, equivalent to second_mircrosecond:
gbase> select date_trunc(microseconds,'2023-04-23 11:12:13.56789');
+------------------------------------------------------+
| date_trunc(microseconds,'2023-04-23 11:12:13.56789') |
+------------------------------------------------------+
| 2023-04-23 11:12:13.567890 |
+------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Going forward, pick the type by the bucket you actually need in gbase SQL rather than by name similarity: week can return a date in the previous year, century and millennium both land on 2001-01-01 here, and milliseconds / microseconds only clear the fractional part. Pinning that choice keeps a gbase database report from grouping rows into the wrong period.
Top comments (0)