DEV Community

Michael
Michael

Posted on

date_part and extract in GBase 8a: Every Type and Its Result

GBase 8a provides date_part to extract parts of a date, plus an Oracle-compatible extract. This post covers both functions in the China-domestically developed GBase 8a MPP database from GBASE, with the complete set of type values from the source examples.

Reference

The companion note on the date_trunc date-truncation function.

Syntax

date_part(type,date)
extract(type from date)
Enter fullscreen mode Exit fullscreen mode
  • type is the part to extract. milliseconds returns a decimal; every other type returns bigint(12).
  • date is normally a datetime; a date value is converted to datetime automatically.
  • date_part accepts the type both with and without quotes, for example year and 'year'. The examples below do not repeat both forms.
  • epoch is not supported by date_part; only extract supports it.

Results by type

Every value below comes from the source examples on the test timestamp 2023-04-23 11:12:13.56789. date_part and extract return the same value for every type except epoch.

type date_part extract note
year 2023 2023
year_month 202304 202304
quarter 2 2
month 4 4
week 17 17 same as week(); the first days of a year may be week 0 or 1 depending on the mode
day 23 23
day_hour 2311 2311 day + hour
day_minute 231112 231112 day + hour + minute
day_second 23111213 23111213 day + hour + minute + second
hour 11 11
hour_minute 1112 1112
hour_second 111213 111213
minute 12 12
minute_second 1213 1213
second 13 13
millisecond 567 567 the 6-digit microsecond value divided by 1000, truncated with no rounding
microsecond 567890 567890 6 digits; apply %1000 to drop the part above 1000
day_microsecond 23111213567890 23111213567890
hour_microsecond 111213567890 111213567890
minute_microsecond 1213567890 1213567890
second_microsecond 13567890 13567890
century 21 21
decade 202 202
dow 0 0 Sunday = 0, Saturday = 6; distinct from isodow
doy 113 113 day of year
epoch unsupported 1682219533.56789 unix_timestamp() returns 1682219533
isodow 7 7 Sunday = 7, Monday = 1
isoyear 2022 / 2025 2022 / 2025 ISO8601 year; see the example below
millennium 3 3
milliseconds 13567.89 13567.89 seconds and milliseconds, in milliseconds, decimal
microseconds 13567890 13567890 seconds to microseconds, in microseconds

Compound types that include microseconds pad the value with trailing zeros when the microsecond part has fewer than 6 digits, so 0.1 and 0.100000 are treated the same.

Selected examples

Both type spellings and the extract form

gbase> select date_part(year,'2023-04-23 11:12:13.56789');
+---------------------------------------------+
| date_part(year,'2023-04-23 11:12:13.56789') |
+---------------------------------------------+
|                                        2023 |
+---------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select date_part('year','2023-04-23 11:12:13.56789');
+-----------------------------------------------+
| date_part('year','2023-04-23 11:12:13.56789') |
+-----------------------------------------------+
|                                          2023 |
+-----------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select extract(year from '2023-04-23 11:12:13.56789');
+------------------------------------------------+
| extract(year from '2023-04-23 11:12:13.56789') |
+------------------------------------------------+
|                                           2023 |
+------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

millisecond and microsecond

gbase> select date_part(millisecond,'2023-04-23 11:12:13.56789');
+----------------------------------------------------+
| date_part(millisecond,'2023-04-23 11:12:13.56789') |
+----------------------------------------------------+
|                                                567 |
+----------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select extract(millisecond from '2023-04-23 11:12:13.56789');
+-------------------------------------------------------+
| extract(millisecond from '2023-04-23 11:12:13.56789') |
+-------------------------------------------------------+
|                                                   567 |
+-------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select date_part(microsecond,'2023-04-23 11:12:13.56789');
+----------------------------------------------------+
| date_part(microsecond,'2023-04-23 11:12:13.56789') |
+----------------------------------------------------+
|                                             567890 |
+----------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select extract(microsecond from '2023-04-23 11:12:13.56789');
+-------------------------------------------------------+
| extract(microsecond from '2023-04-23 11:12:13.56789') |
+-------------------------------------------------------+
|                                                567890 |
+-------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

epoch: only extract supports it

gbase> select date_part(epoch,'2023-04-23 11:12:13.56789');
ERROR 1733 (HY000): (GBA-01EX-700) Gbase general error: This version of GBase doesn't yet support 'EPOCH in date_part
gbase>
gbase> select extract(epoch from '2023-04-23 11:12:13.56789') a,unix_timestamp('2023-04-23 11:12:13.56789') b;
+------------------+------------+
| a                | b          |
+------------------+------------+
| 1682219533.56789 | 1682219533 |
+------------------+------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

extract keeps the fractional seconds that unix_timestamp() drops.

isoyear can differ from year

Under ISO8601 the week containing the first Thursday is week one, and the week containing the last Thursday is the final week, so dates near a year boundary can belong to the previous or next year:

gbase> select date_part(isoyear,'2023-01-01 11:12:13.56789') isoyear,date_part(year,'2023-01-01 11:12:13.56789') year;
+---------+------+
| isoyear | year |
+---------+------+
|    2022 | 2023 |
+---------+------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select extract(isoyear from '2023-01-01 11:12:13.56789') isoyear,extract(year from '2023-01-01 11:12:13.56789') year;
+---------+------+
| isoyear | year |
+---------+------+
|    2022 | 2023 |
+---------+------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select date_part(isoyear,'2024-12-31 11:12:13.56789');
+------------------------------------------------+
| date_part(isoyear,'2024-12-31 11:12:13.56789') |
+------------------------------------------------+
|                                           2025 |
+------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

The decimal-returning type

gbase> select date_part(milliseconds,'2023-04-23 11:12:13.56789');
+-----------------------------------------------------+
| date_part(milliseconds,'2023-04-23 11:12:13.56789') |
+-----------------------------------------------------+
|                                            13567.89 |
+-----------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select extract(milliseconds from '2023-04-23 11:12:13.56789');
+--------------------------------------------------------+
| extract(milliseconds from '2023-04-23 11:12:13.56789') |
+--------------------------------------------------------+
|                                               13567.89 |
+--------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

Going forward, pick the type by the unit you actually need in gbase SQL rather than by name similarity: millisecond, milliseconds, microsecond and microseconds are four different results, dow and isodow disagree on Sunday, and epoch will error in date_part. Pinning those choices keeps a gbase database report from silently shifting by a year or a factor of a thousand.

Top comments (0)