Usage of trunc() function in Oracle

Usage of Oracle trunc() function

1.TRUNC(for dates)

  The date value truncated by the TRUNC function for the specified element.

  Its specific syntax is as follows:

  TRUNC(date[,fmt])

  in:

  date a date value

  fmt Date format, the date will be truncated by the specified element format. Omit it to truncate by the most recent date

  Here's how the function is used:

  TRUNC(TO_DATE('24-Nov-1999 08:00 pm'),'dd-mon-yyyy hh:mi am')

  ='24-Nov-1999 12:00:00 am'

  TRUNC(TO_DATE('24-Nov-1999 08:37 pm','dd-mon-yyyy hh:mi am'),'hh') ='24-Nov-1999 08:00:00 am'

  trunc(sysdate,'yyyy') -- returns the first day of the current year.

  trunc(sysdate,'mm') -- returns the first day of the current month.

  trunc(sysdate,'d') -- Returns the first day of the current week.

  trunc(sysdate,'dd')--returns the current year, month and day

  2.TRUNC(for number)

  The TRUNC function returns the processed value, and its working mechanism is very similar to the ROUND function, except that this function does not perform corresponding rounding selection processing for the part before or after the specified decimal, but truncates it all.

  Its specific syntax is as follows

  TRUNC(number[,decimals])

  in:

  number The value to be intercepted

  decimals specifies the number of digits after the decimal point to be retained. optional, omit it to truncate all fractional parts

  Here's how the function is used:

  TRUNC(89.985,2)=89.98

  TRUNC(89.985)=89

  TRUNC(89.985,-1)=80

  Note: The second parameter can be a negative number, which means that the part after the specified number of digits to the left of the decimal point is truncated, that is, it is marked as 0. Similar to rounding, for example, if the parameter is 1, it is rounded to the tenth place, if it is -1, it is rounded to the tenth place, and so on.

 

--Usage of Oracle trunc() function
/****************Date********************/
1.select trunc (sysdate) from dual  --2011-3-18 Today's date is 2011-3-18
2.select trunc(sysdate, 'mm') from dual  --2011-3-1 returns the first day of the current month.
3.select trunc(sysdate,'yy') from dual   --2011-1-1 returns the first day of the
current year4.select trunc(sysdate,'dd') from dual   --2011-3-18 returns the current year month
day5.select trunc(sysdate,'yyyy') from dual   --2011-1-1 returns the first day of the current year
6.select trunc(sysdate,'d') from dual   --2011-3-13 (Sunday) returns the first day of the current week One day
7.select trunc(sysdate, 'hh') from dual    --2011-3-18 14:00:00 The current time is 14:41  
8.select trunc(sysdate, 'mi') from dual  --2011- 3-18 14:41:00 The TRUNC() function has no second precision
/****************Number********************/
/*
TRUNC(number,num_digits)
Number needs to be truncated Rounded number.
Num_digits Numbers used to specify rounding precision. The default value of Num_digits is 0.
TRUNC() function does not round off when intercepting
*/
9.select trunc(123.458) from dual --123
10.select trunc(123.458,0) from dual --123
11.select trunc(123.458,1) from dual -- 123.4
12.select trunc(123.458,-1) from dual --120
13.select trunc(123.458,-4) from dual --0
14.select trunc(123.458,4) from dual   --123.458
15.select trunc( 123) from dual   --123
16.select trunc(123,1) from dual --123
17.select trunc(123,-1) from dual --120

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326100796&siteId=291194637