The Oracle PL/SQL TRUNC function is used to truncate a date value to a specific level of precision. This function is commonly used to remove the time portion of a date value and return only the date component.
Syntax
The syntax to truncate a date with TRUNC function is as follows:
TRUNC(date, [format])
Where:
date: the input date value to be truncated
format: the optional parameter that specifies the level of precision to which the date value is to be truncated. It can be one of the following values:
‘YYYY’: Year
‘MM’: Month
‘DD’: Day (default value)
‘HH24’: Hour
‘MI’: Minute
Examples
Truncate a date to remove the time portion:
SELECT TRUNC(SYSDATE) FROM dual; Output: 09-APR-2023
Truncate a date to the nearest month:
SELECT TRUNC(SYSDATE, 'MM') FROM dual; Output: 01-APR-2023
Truncate a date to the nearest year:
SELECT TRUNC(SYSDATE, 'YYYY') FROM dual; Output: 01-JAN-2023
In conclusion, the TRUNC function in Oracle PL/SQL is a powerful tool for manipulating date values, allowing developers to easily truncate a date to a specific level of precision.