獲取系統(tǒng)當前時間
date類型的
select sysdate from dual;
char類型的
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; select to_char(sysdate, 'yyyy' ) from dual; --年 select to_char(sysdate, 'MM' ) from dual; --月 select to_char(sysdate, 'dd' ) from dual; --日 select to_char(sysdate, 'Q') from dual; --季 select to_char(sysdate, 'iw') from dual; --周--按日歷上的那種,每年有52或者53周
日期操作
當前時間減去7分鐘的時間 select sysdate,sysdate - interval '7' MINUTE from dual; 當前時間減去7小時的時間 select sysdate - interval '7' hour from dual; 當前時間減去7天的時間 select sysdate - interval '7' day from dual; 當前時間減去7月的時間 select sysdate,sysdate - interval '7' month from dual; 當前時間減去7年的時間 select sysdate,sysdate - interval '7' year from dual; 時間間隔乘以一個數(shù)字 select sysdate,sysdate - 8*interval '7' hour from dual;
常用的時間戳
//獲取當年的一月一號 to_date(concat((select to_char(sysdate,'yyyy') from dual), '-01-01 00:00:00'),'yyyy-MM-dd HH24:mi:ss') //date格式 //獲取這個月的一月一號 SELECT LAST_DAY(ADD_MONTHS(SYSDATE, -1)) + 1 FROM DUAL; //date格式 SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)) + 1,'yyyy-mm-dd HH24:mi:ss') FROM DUAL; //char格式
trunc()函數(shù)
trunc函數(shù)處理數(shù)字
該函數(shù)不對指定小數(shù)前或后的部分做相應舍入選擇處理,而統(tǒng)統(tǒng)截去。
其具體的語法格式如下
TRUNC(number[,decimals])
其中:
number 待做截取處理的數(shù)值
decimals 指明需保留小數(shù)點后面的位數(shù)??蛇x項,忽略它則截去所有的小數(shù)部分。
1 select trunc(123.98)from dual; 2 select trunc(123.123,2)from dual; 3 select trunc(123.123,-1)from dual;
注意:第二個參數(shù)可以為負數(shù),表示為小數(shù)點左邊指定位數(shù)后面的部分截去,即均以0記。與取整類似,比如參數(shù)為1即取整到十分位,如果是-1,則是取整到十位,以此類推;如果所設置的參數(shù)為負數(shù),且負數(shù)的位數(shù)大于或等于整數(shù)的字節(jié)數(shù)的話,則返回為0。如:TRUNC(89.985,-3)=0。
trunc函數(shù)處理日期
trunc函數(shù)返回以指定元元素格式截去一部分的日期值。
其具體的語法格式如下:
TRUNC(date,[fmt])
其中:
date為必要參數(shù),是輸入的一個日期值
fmt參數(shù)可忽略,是日期格式,用以指定的元素格式來截去輸入的日期值。忽略它則由最近的日期截去
下面是該函數(shù)的使用情況:
1 trunc(sysdate,'yyyy') --返回當年第一天. 2 trunc(sysdate,'mm') --返回當月第一天. 3 trunc(sysdate,'d') --返回當前星期的第一天. 4 select trunc(sysdate,'YYYY')from dual; 5 select trunc(sysdate,'MM')from dual; 6 select trunc(sysdate,'D')from dual;
/**************日期********************/ 1.select trunc(sysdate) from dual --2013-01-06 今天的日期為2013-01-06 2.select trunc(sysdate, 'mm') from dual --2013-01-01 返回當月第一天. 3.select trunc(sysdate,'yy') from dual --2013-01-01 返回當年第一天 4.select trunc(sysdate,'dd') from dual --2013-01-06 返回當前年月日 5.select trunc(sysdate,'yyyy') from dual --2013-01-01 返回當年第一天 6.select trunc(sysdate,'d') from dual --2013-01-06 (星期天)返回當前星期的第一天 7.select trunc(sysdate, 'hh') from dual --2013-01-06 17:00:00 當前時間為17:35 8.select trunc(sysdate, 'mi') from dual --2013-01-06 17:35:00 TRUNC()函數(shù)沒有秒的精確 /***************數(shù)字********************/ /* TRUNC(number,num_digits) Number 需要截尾取整的數(shù)字。 Num_digits 用于指定取整精度的數(shù)字。Num_digits 的默認值為 0。 TRUNC()函數(shù)截取時不進行四舍五入 */ 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
round函數(shù)(四舍五入)
描述 : 傳回一個數(shù)值,該數(shù)值是按照指定的小數(shù)位元數(shù)進行四舍五入運算的結果。
SELECT ROUND( number, [ decimal_places ] ) FROM DUAL
參數(shù):
number : 欲處理之數(shù)值
decimal_places : 四舍五入 , 小數(shù)取幾位 ( 預設為 0 )
1 Sample : 2 select round(123.456, 0) from dual; 回傳 123 3 select round(123.456, 1) from dual; 回傳 123.5 4 select round(-123.456, 2) from dual; 回傳 -123.46
ceil和floor函數(shù)
ceil和floor函數(shù)在一些業(yè)務數(shù)據(jù)的時候,有時還是很有用的。
ceil(n) 取大于等于數(shù)值n的最小整數(shù);
floor(n)取小于等于數(shù)值n的最大整數(shù);
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。