oracle trunc() truncation function

Date and number can be intercepted

For example, 2010-10-15, cut to the first day of 2010, that is, 2010-01-01,
such as 20.5, cut to 20

Cut-off date

You must first understand what the cut-off date is.
Assuming date=27-10-20
trunc(date,'yyyy'), get the first day of the year

Different from to_char();

如果 2020-05-12,你想获取2020,请用`to_char(date,'yyyy');`
获取05,`to_char(date,'mm')`
获取12,`to_char(date,'dd')`

trunc(date [, fmt ]) , return date

  1. fmt omit
    output year, month and day
  2. yyyy
    returns to the first day of the year等于 SYYYY YYYY YEAR SYEAR YYY YY Y
    Insert picture description here
  3. mm, the first day of the month. equalMONTH MON MM RM
    Insert picture description here
  4. ww. A date with the same day of the week as the first day of the year.
    For example, 2020-12-23, the first day of the first year is Wednesday, and 2020-12-23 happens to be Wednesday, output it;
    2020-12-22, the first day of the first year is Wednesday, output 2020-12-16 (The 16th is Wednesday) The
    output date <= the specified date.
  5. DD, annual income, equal toDDD DD J 和省略fmt
  6. day, the first day of the week (Sunday), equal toDAY DY D

Intercepted numbers

It's just interception, not rounding up.
trunc(n1,[,n2]) , return number

  1. n2 can be omitted. Intercept the integer part like this
    Insert picture description here

  2. n2 is positive, truncated to n2 digits after the decimal point
    Insert picture description here

  3. N2 digits are negative, n2 digits before the decimal point are set to 0
    Insert picture description here

Reference from: oracle official document
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/TRUNC-number.html#GUID-911AE7FE-E04A-471D-8B0E-9C50EBEFE07D

Guess you like

Origin blog.csdn.net/qq_43179428/article/details/109313449