oracle查詢截至到當前日期月份所在年份的所有月份

這篇文章主要介紹了oracle查詢截至到當前日期月份所在年份的所有月份,本文通過代碼給大家介紹的非常詳細,具有一定的參考借鑑價值 ,需要的朋友可以參考下

下面通過一個查詢語句給大家介紹oracle查詢截至到當前日期月份所在年份的所有月份,具體代碼如下所示:

SELECT to_number(TO_CHAR(add_months(trunc(sysdate, 'yy'), ROWNUM - 1), 'MM')) as month
 FROM DUAL
CONNECT BY ROWNUM <=
 (select months_between(trunc(sysdate, 'mm'), trunc(sysdate, 'yy')) + 1
 from dual);

當然,也可以指定具體的時間段,只要把months_between裏面的兩個日期改成具體的日期就行,

其中,trunc(sysdate, 'mm')是返回當月的第一天,trunc(sysdate, 'yy')是返回當年的第一天。

擴展知識點 Oracle trunc()函數的用法

/**************日期********************/
select trunc(sysdate) from dual --2013-01-06 今天的日期爲2013-01-06
select trunc(sysdate, 'mm') from dual --2013-01-01 返回當月第一天.
select trunc(sysdate,'yy') from dual --2013-01-01 返回當年第一天
select trunc(sysdate,'dd') from dual --2013-01-06 返回當前年月日
select trunc(sysdate,'yyyy') from dual --2013-01-01 返回當年第一天
select trunc(sysdate,'d') from dual --2013-01-06 (星期天)返回當前星期的第一天
select trunc(sysdate, 'hh') from dual --2013-01-06 17:00:00 當前時間爲17:35 
select trunc(sysdate, 'mi') from dual --2013-01-06 17:35:00 TRUNC()函數沒有秒的精確
/***************數字********************/
/*
TRUNC(number,num_digits) 
Number 需要截尾取整的數字。 
Num_digits 用於指定取整精度的數字。Num_digits 的默認值爲 0。
TRUNC()函數截取時不進行四捨五入
*/
select trunc(123.458) from dual --123
.select trunc(123.458,0) from dual --123
.select trunc(123.458,1) from dual --123.4
.select trunc(123.458,-1) from dual --120
.select trunc(123.458,-4) from dual --0
.select trunc(123.458,4) from dual --123.458
.select trunc(123) from dual --123
.select trunc(123,1) from dual --123
.select trunc(123,-1) from dual --120

總結

以上所述是小編給大家介紹的oracle查詢截至到當前日期月份所在年份的所有月份,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章