Usage of TRUNC (interception) function

The TRUNC function is used in Oracle to truncate the precision of a date, time, or number. Its syntax is as follows:

Intercept numbers:

TRUNC(number [, precision])

in:

  • number represents the number to truncate.

  • precision represents the precision of truncation. Can be a negative number, an integer, or empty by default. A positive number means retaining the number of decimal places, a negative number means truncating the number of decimal places, and 0 means truncating the integer part.

    Here are some examples:

SELECT TRUNC(12.345) FROM dual; -- 返回12
SELECT TRUNC(9.999, 2) FROM dual; -- 返回9.99
SELECT TRUNC(1234.5678, -2) FROM dual; -- 返回1200
SELECT TRUNC(1234.5678, 1) FROM dual; -- 返回1234.5

Cutoff date

TRUNC(date [, format])

in

  • date represents the date/time to be truncated.
  • format represents the format of the truncated result. Can be one of the following values: 'YYYY', 'YYY', 'YY', 'YEAR','MONTH', 'MON', 'MM', 'DY', 'DAY', 'HH24', 'HH12' , 'HH', 'MI', 'SS', 'SSSSS' etc.

Here are some examples:

select trunc(sysdate) from dual; -- 2023-08-04 今天的日期为2023-08-04
select trunc(sysdate, 'mm') from dual; -- 2023-08-01 返回当月第一天
select trunc(sysdate, 'MONTH') from dual;  -- 2023-08-01 返回当月第一天
select trunc(sysdate, 'yy') from dual; -- 2023-01-01 返回当年第一天
select trunc(sysdate, 'yyyy') from dual; -- 2023-01-01 返回当年第一天
SELECT TRUNC(SYSDATE, 'YYYY') FROM dual; -- 2023-01-01 返回当年第一天
select trunc(sysdate, 'dd') from dual; -- 2023-08-04 返回当前年月日
select trunc(sysdate, 'd') from dual; -- 2023-07-30 (星期天)返回当前星期的第一天,自己查下日历看下
select trunc(sysdate, 'day') from dual; -- 返回当前星期的第一天
select trunc(sysdate, 'hh') from dual; -- 2023-08-04 10:00:00 返回当前小时的开始时间 当前时间为10:15
SELECT TRUNC(SYSDATE, 'HH24') FROM dual; -- 返回当前小时的开始时间
select trunc(sysdate, 'mi') from dual; -- 2023-08-04 10:15:00 返回当前分钟的开始时间 TRUNC()函数没有秒的精确
SELECT TRUNC(SYSDATE, 'MI') FROM dual; -- 返回当前分钟的开始时间

Please note that the TRUNC function may be used differently in Oracle and MySQL. The above example only applies to Oracle databases. When using, please consult the documentation of the database you are using for specific syntax and usage.

Here are some corresponding writing methods for mysql, because mysql does not have the TRUNC function.

In MySQL, you can use the DATE_FORMAT function and STR_TO_DATE function to achieve functions similar to the TRUNC function in Oracle. Here's an example:

SELECT DATE_FORMAT(sysdate(), '%Y-%m-%d') from dual; -- 2023-08-04 返回当前年月日
SELECT DATE_FORMAT(sysdate(), '%Y-%m-01') from dual; -- 2023-08-01 返回当月第一天
SELECT DATE_FORMAT(sysdate(), '%Y-01-01') from dual; -- 2023-01-01 返回当年第一天

Note that there is no direct equivalent in MySQL to the TRUNC function in Oracle. Therefore, you need to use STR_TO_DATE to convert the string to a date, and then use the DATE_FORMAT function to specify the desired date format to achieve truncation.

If you have a better way, please leave a message in the comment area.

Guess you like

Origin blog.csdn.net/qq_44275015/article/details/132098089
Recommended