好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

oracle查询截至到当前日期月份所在年份的所有月份

下面通过一个查询语句给大家介绍oracle查询截至到当前日期月份所在年份的所有月份,具体代码如下所示:

?

1

2

3

4

5

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()函数的用法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

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查询截至到当前日期月份所在年份的所有月份,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/myyard/archive/2019/07/30/ora_month_qry.html

查看更多关于oracle查询截至到当前日期月份所在年份的所有月份的详细内容...

  阅读:31次