Friday, January 11, 2013

Trunc Function (with dates)


In Oracle/PLSQL, the trunc function returns a date truncated to a specific unit of measure.

Syntax

The syntax for the trunc function is:
trunc ( date, [ format ] )
date is the date to truncate.
format is the unit of measure to apply for truncating. If the format parameter is omitted, the trunc function will truncate the date to the day value, so that any hours, minutes, or seconds will be truncated off.
Below are the valid format parameters:
UnitValid format parameters
YearSYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y
ISO YearIYYY, IY, I
QuarterQ
MonthMONTH, MON, MM, RM
WeekWW
IWIW
WW
DayDDD, DD, J
Start day of the weekDAY, DY, D
HourHH, HH12, HH24
MinuteMI

Applies To

  • Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

For Example

trunc(to_date('22-AUG-03'), 'YEAR')would return '01-JAN-03'
trunc(to_date('22-AUG-03'), 'Q')would return '01-JUL-03'
trunc(to_date('22-AUG-03'), 'MONTH')would return '01-AUG-03'
trunc(to_date('22-AUG-03'), 'DDD')would return '22-AUG-03'
trunc(to_date('22-AUG-03'), 'DAY')would return '17-AUG-03'