Usage
1.TRUNC (for dates) Date action
The date value that the Trunc function truncates for the specified element.
Its specific syntax format is as follows:
TRUNC (Date[,fmt])
which
Date value
FMT the date format, which is truncated by the specified element format. Ignoring it is truncated by the nearest date.
The following is the use of this function:
| The code is as follows |
Copy Code |
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 ')--return to the first day of the year.
Trunc (sysdate, ' mm ')--return to the first day of the month.
Trunc (sysdate, ' d ')--Returns the first day of the current week.
Trunc (sysdate, ' DD ')--return to current month day |
Example 1
| The code is as follows |
Copy Code |
|
/************** 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 month.
3.select trunc (sysdate, ' yy ') from dual--2011-1-1 returns the first day of the year
4.select trunc (sysdate, ' DD ') from dual--2011-3-18 back to the current month day
5.select trunc (sysdate, ' yyyy ') from dual--2011-1-1 return to the first day of the year
6.select trunc (sysdate, ' d ') from dual--2011-3-13 (Sunday) returns the first day of the current week
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 trunc () function without precision of seconds |
2.TRUNC (for number) digital operation
The TRUNC function returns the processed value, which works very much like the round function, except that the function does not round the specified decimal number before or after the appropriate rounding selection.
Its specific syntax format is as follows
TRUNC (Number[,decimals])
which
Number to be intercepted
Decimals indicates the number of digits that need to be retained after the decimal point. Optional, ignore it to intercept all the decimal parts
The following is the use of this function:
/*
TRUNC (Number,num_digits)
Number needs to be truncated and rounded.
Num_digits is used to specify the number of rounding precision. The default value for Num_digits is 0.
TRUNC () function is truncated without rounding
*/
| The code is as follows |
Copy Code |
|
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 |
Note: The second argument can be a negative number, represented as a portion of the following part of the specified digit to the left of the decimal point, that is, 0. Similar to rounding, for example, 1 is the integer to the very bit, if it is 1, it is rounded to 10 digits, and so on.