Friday, January 11, 2013

Round Function (with dates)


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

Syntax

The syntax for the round function is:
round( date, [ format ] )
date is the date to round.
format is the unit of measure to apply for rounding. If the format parameter is omitted, the round function will round to the nearest day.
Below are the valid format parameters:
UnitValid format parametersRounding Rule
YearSYYYY, YYYY, YEAR, SYEAR, YYY, YY, YRounds up on July 1st
ISO YearIYYY, IY, I 
QuarterQRounds up on the 16th day of the second month of the quarter
MonthMONTH, MON, MM, RMRounds up on the 16th day of the month
WeekWWSame day of the week as the first day of the year
IWIWSame day of the week as the first day of the ISO year
WWSame day of the week as the first day of the month
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

round(to_date ('22-AUG-03'),'YEAR')would return '01-JAN-04'
round(to_date ('22-AUG-03'),'Q')would return '01-OCT-03'
round(to_date ('22-AUG-03'),'MONTH')would return '01-SEP-03'
round(to_date ('22-AUG-03'),'DDD')would return '22-AUG-03'
round(to_date ('22-AUG-03'),'DAY')would return '24-AUG-03'