tech on the net

Oracle/PLSQL: TRUNC Function (with dates)

Learn how to use the Oracle/PLSQL TRUNC function (as it applies to date values) with syntax and examples.

Description

The Oracle/PLSQL TRUNC function returns a date truncated to a specific unit of measure.

Syntax (with dates)

The syntax for the Oracle/PLSQL TRUNC function is:

TRUNC ( date, [ format ] )

Parameters or Arguments

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:

Unit Valid format parameters
Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y
ISO Year IYYY, IY, I
Quarter Q
Month MONTH, MON, MM, RM
Week WW
IW IW
W W
Day DDD, DD, J
Start day of the week DAY, DY, D
Hour HH, HH12, HH24
Minute MI

Applies To

The TRUNC function can be used in the following versions of Oracle/PLSQL:

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

Example - with dates

Let's look at some Oracle TRUNC function examples and explore how you would use the TRUNC function in Oracle/PLSQL.

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'