Home > Database > Oracle > body text

Usage of trunc function in oracle

下次还敢
Release: 2024-04-30 06:09:15
Original
517 people have browsed it

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>
Copy after login

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>
Copy after login

Truncate number

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

Truncation combination

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!