DTRUNC: Returning the Start of a Date Period for a Given Date

How to:

Given a date or timestamp and a component, DTRUNC returns the first date within the period specified by that component.

Syntax: How to Return the First or Last Date of a Date Period

DTRUNC(date_or_timestamp, date_period)

where:

date_or_timestamp

Date or date-time

Is the date or timestamp of interest.

date_period

Is the period whose starting or ending date you want to find. Can be one of the following:

  • DAY, returns the date that represents the input date (truncates the time portion, if there is one).
  • YEAR, returns the date of the first day of the year.
  • MONTH, returns the date of the first day of the month.
  • QUARTER, returns the date of the first day in the quarter.
  • WEEK, returns the date that represents the first date of the given week.

    By default, the first day of the week will be Sunday, but this can be changed using the WEEKFIRST parameter.

  • YEAR_END, returns the last date of the year.
  • QUARTER_END, returns the last date of the quarter.
  • MONTH_END, returns the last date of the month.
  • WEEK_END, returns the last date of the week.

Example: Returning the First Date in a Date Period

DTRUNC returns the first date of the quarter given the date of birth:

DTRUNC(DATE_OF_BIRTH,QUARTER)

For 1993/03/27, the result is 1993/03/01.

Example: Using the Start of Week Parameter for DTRUNC

DTRUNC returns the date that represents the start of the week.

DTRUNC(START_DATE, WEEK)

For 2013/01/15, the result is 2013/01/13

Example: Returning the Date of the Last Day of a Week

DTRUNC calculates the date of the end of the week.

WEEKEND/YYMD = DTRUNC(START_DATE, WEEK_END)

For 2013/01/15, the result is 2013/01/19.

WebFOCUS

Feedback