4. Elements of OpenSQL Statements : Functions : Scalar Functions : Date_trunc Function
 
Share this page                  
Date_trunc Function
Use the date_trunc function to group all the dates in the same month or year, and so forth. For example:
date_trunc('month',date('23-oct-1998 12:33'))
returns 1-oct-1998, and
date_trunc('year',date('23-oct-1998'))
returns 1-jan-1998.
Truncation takes place in terms of calendar years and quarters (1-jan, 1-apr, 1-jun, and 1-oct).
To truncate in terms of a fiscal year, offset the calendar date by the number of months between the beginning of your fiscal year and the beginning of the next calendar year (6 mos for a fiscal year beginning July 1, or 4 mos for a fiscal year beginning September 1):
date_trunc('year',date+'4 mos') - '4 mos'
Weeks start on Monday. The beginning of a week for an early January date may fall into the previous year.