1. 程式人生 > 資料庫 >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查詢截至到當前日期月份所在年份的所有月份,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!