Get the App
SLTechnology News&Howtos  ›  Servers  › 

The usage of trunc function

Shulou Source: shulou.com Published: 2022-06-02 07:30:08 11月17日 Update

The usage of TRUNC function

The TRUNC function is used to truncate values.

There are two uses: TRUNC (NUMBER) represents the truncation number, and TRUNC (date) represents the truncation date.

(1) truncation figures:

Format: TRUNC (N1 ~ N2), N1 represents the truncated number, N2 indicates the bit to be truncated. N2 can be a negative number, which means before the truncated decimal point. Note that TRUNC truncation is not rounded.

SQL > select TRUNC (15.79) from dual

TRUNC (15.79)

-

fifteen

SQL > select TRUNC (15.79jue 1) from dual

TRUNC (15.79 and 1)

-

15.7

SQL > select trunc (15.79) from dual

TRUNC (15.79 Mai Mui 1)

-

ten

(2) cut-off date:

Execute the command first: alter session set nls_date_format='yyyy-mm-dd hh34:mi:hh'

Intercept today:

SQL > select sysdate,trunc (sysdate,'dd') from dual

SYSDATE TRUNC (SYSDATE,'DD')

--

2009-03-24 21:31:17 2009-03-24 00:00:00

Intercept the first day of the week:

SQL > select sysdate,trunc (sysdate,'d') from dual

SYSDATE TRUNC (SYSDATE,'D')

--

2009-03-24 21:29:32 2009-03-22 00:00:00

Intercept the first day of the month:

SQL > select sysdate,trunc (sysdate,'mm') from dual

SYSDATE TRUNC (SYSDATE,'MM')

--

2009-03-24 21:30:30 2009-03-01 00:00:00

Intercept the first day of this year:

SQL > select sysdate,trunc (sysdate,'y') from dual

SYSDATE TRUNC (SYSDATE,'Y')

--

2009-03-24 21:31:57 2009-01-01 00:00:00

Intercept to the hour:

SQL > select sysdate,trunc (sysdate,'hh') from dual

SYSDATE TRUNC (SYSDATE,'HH')

--

2009-03-24 21:32:59 2009-03-24 21:00:00

Intercept to minutes:

SQL > select sysdate,trunc (sysdate,'mi') from dual

SYSDATE TRUNC (SYSDATE,'MI')

--

2009-03-24 21:33:32 2009-03-24 21:33:00

Get the first day of last month:

SQL > select TRUNC (add_months (SYSDATE,-1), 'MM') from dual

Tags: Number function date rounding command decimal decimal hour year format negative rounding Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Xiaomi Linux Huawei