Home  >  Article  >  Database  >  Usage of trunc function in oracle

Usage of trunc function in oracle

下次还敢
下次还敢Original
2024-04-30 06:09:15566browse

The TRUNC function in Oracle is used to truncate the specified part of the date or number. It supports the following units: year, month, date, hour, minute, second. The syntax is TRUNC(expression, unit), expression is the expression to be truncated, and unit is the unit to be truncated.

Usage of trunc function in oracle

Usage of TRUNC function in Oracle

Brief Overview

TRUNC Function used in Oracle to truncate a specified portion from a date or number.

Syntax

<code>TRUNC(<expression>, <unit>)</code>

Parameters

  • expression: The expression to be truncated, Can be a date or number.
  • unit: The unit to be truncated, which can be:

    • YEAR: truncated to the year
    • MONTH: truncated to the month
    • DAY: Truncated to days
    • HOUR: Truncated to hours
    • MINUTE: Truncated to minutes
    • SECOND: Truncated to seconds

Return value

The TRUNC function returns a truncated value whose type is the same as the expression.

Usage

Truncate date

<code>SELECT TRUNC(sysdate, 'YEAR') FROM dual;
-- 返回当前年份,如 2023</code>

Truncate number

<code>SELECT TRUNC(123.456, 2) FROM dual;
-- 返回截断到小数点后两位的数字,如 123.45</code>

Truncation combination

<code>SELECT TRUNC(sysdate, 'DAY TO SECOND') FROM dual;
-- 返回当前日期和时间,截断到秒</code>

Notes

  • The TRUNC function truncates the specified part and all parts after it.
  • If the unit parameter is not a valid unit, the function will return an error.
  • If expression is NULL, the function will return NULL.

The above is the detailed content of Usage of trunc function in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn