TIME_BUCKET (datetime)

Syntax

Description of the illustration time_bucket.gif

timebucket_optional_clause::=

Description of the illustration timebucket_optional_clause.gif

Purpose

Use TIME_BUCKET(datetime) to obtain the datetime over an interval that you specify.

TIME_BUCKET has three required arguments, and two optional arguments .

Rules

For details on dependencies with NLS_LANGUAGE and NLS_CALENDAR see Database Globalization Support Guide

Examples

The following examples use the NLS_DATE_FORMAT YYYY-MM-DD. Set the date format with ALTER SESSION:

ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD';

Example 1

SELECT TIME_BUCKET (DATE '2022-06-29', INTERVAL '5' YEAR, DATE '2000-01-01', START);

The result is:

2020-01-01

The 5-year time bucket that contains 2022-06-29 is from 2020-01-01(start) to 2025- 01-01(end). The fourth argument START is used, so the start of the time bucket 2020-01-01 is returned.

Example 2

The following two queries are equivalent:

SELECT TIME_BUCKET ( DATE '-2022-06-29', 'P5M', DATE '-2022-01-01', END );

Or:

SELECT TIME_BUCKET ( DATE '-2022-06-29', INTERVAL '5' MONTH, DATE '-2022-01-01', END);

The result is:

-2022-11-01

The 5-month time bucket that contains -2022-06-29 is from 2022-06-01(start) to -2022- 11-01(end). The fourth argument END is used, so the end of the time bucket 2022-11-01 is returned.

Example 3

SELECT TIME_BUCKET ( DATE '2005-03-10', 'P1Y', DATE '2004-02-29' ON OVERFLOW ERROR );

The result is:

ORA-01839: date not valid for month specified

The one-year time bucket that contains '2005-03-10' is from error (or '2005-02-29') (start) to error (or '2006-02-29') (end). Default fourth argument START is used, so the start of the time bucket should be returned which is an error.

Example 4

SELECT TIME_BUCKET ( DATE '2005-03-10', 'P1Y', DATE '2004-02-29' ON OVERFLOW ROUND );

The result is:

2005-02-28

The one-year time bucket that contains '2005-03-10' is from '2005-02-28'(start) to '2006- 02-28'(end) since February 29 is rounded to February 28. Default fourth argument START is used, so the start of the time bucket is returned: '2005-02-28'.

Example 5

SELECT TIME_BUCKET ( DATE '2004-04-02', 'P1Y', DATE '2003-02-28' LAST DAY OF MONTH );

The result is:

2004-02-29

The one-year time bucket that contains '2003-02-28' is from '2004-02-29'(start) to '2005- 02-28'(end) since '2004-02-28' is rounded to the last day of that month which is '2004-02-29'. Default fourth argument START is used, so the start of the time bucket is returned: '2004-02- 29'.