oracle截斷函式(日期與數值)

2021-06-18 20:33:39 字數 2605 閱讀 3048

語法為round(number,num_digits),第乙個引數為截斷的數值,第二個引數為需要的截斷位置,正數代表保留小數點後幾位,例如round(2.17,1)表示保留2.1,0.1後的數值

進行四捨五入操作,故結果為2.2。

其中number是需要進行四捨五入的數字;num_digits為指定的位數,按此位數進行四捨五入,如果 num_digits 大於 0,則四捨五入到指定的小數字,如果 num_digits 等於 0,則四捨五入到最接近的整數,如果 num_digits 小於 0,則在小數點左側進行四捨五入。

例如:round(2.149, 0) 將 2.149 四捨五入到乙個整數結果為2。

round(2.15, 1) 將 2.15 四捨五入到乙個小數字,結果為2.2。

round(2.149, 1) 將 2.149 四捨五入到乙個小數字結果為2.1。

round(-1.475, 2) 將 -1.475 四捨五入到兩小數字結果為-1.48)。

round(21.5, -1) 將 21.5 四捨五入到小數點左側一位結果為20。

sqlplus下演示:

sql> select round(24.946,0) from dual;

round(24.946,0)

---------------25

sql> select round(24.946,1) from dual;

round(24.946,1)

---------------

24.9

sql> select round(24.946,2) from dual;

round(24.946,2)

---------------

24.95

sql> select round(24.946,-1) from dual;

round(24.946,-1)

----------------20

sql> select round(24.946,-2) from dual;

round(24.946,-2)

----------------0

sql> select round(26.946,-1) from dual;

round(26.946,-1)

----------------30

--oracle trunc()函式的用法

/**************日期********************/

1.select trunc(sysdate) from dual  

--2011-3-18  今天的日期為2011-3-18

2.select trunc(sysdate, 'mm')   from   dual  

--2011-3-1    返回當月第一天.

3.select trunc(sysdate,'yy') from dual

--2011-1-1       返回當年第一天

4.select trunc(sysdate,'dd') from dual

--2011-3-18    返回當前年月日

5.select trunc(sysdate,'yyyy') from dual

--2011-1-1   返回當年第一天

6.select trunc(sysdate,'d') from dual

--2011-3-13 (星期天)返回當前星期的第一天

7.select trunc(sysdate, 'hh') from dual 

--2011-3-18 14:00:00   當前時間為14:41   

8.select trunc(sysdate, 'mi') from dual 

--2011-3-18 14:41:00   trunc()函式沒有秒的精確

/***************數字********************/

/*trunc(number,num_digits) 

number 需要截尾取整的數字。 

num_digits 用於指定取整精度的數字。num_digits 的預設值為 0。

trunc()函式擷取時

不進行四捨五入

*/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

--014.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

oracle 單行函式 日期函式

1.按天加 2.month between準確計算日期相差月份 select hire date,trunc sysdate hire date as 日期相減後,3.add months 月份新增 next day 得到下週某天的日期 last day 得到某月最後一天 select add mo...

oracle系統函式(日期函式)

oracle系統函式 日期函式 呼叫日期函式時,首先要明確兩個概念,時間戳和日期是不同的,日期中包括年月日但不包括小時分鐘秒,時間戳包括年月日小時分鐘秒。在oracle中,一般情況下,函式中包含 date字元的和日期有關,包含timestamp的函式和時間戳有關 時間戳可以理解為時間 oracle中...

oracle 日期常用函式 日期運算

oracle 日期常用函式 日期運算 1日期運算 23 1.更改日期顯示的format 4ex.5alter session setnls date format yyyy mm dd 6階段作業已被更改 78 select sysdate from dual 910 sysdate 11 1220...