Datetime Functions

Datetime Functions

Datetime functions operate on values of the DATE datatype. All datetime functions return a datetime or interval value of DATE datatype, except the MONTHS_BETWEEN function, which returns a number. The datetime functions are:

ADD_MONTHS

Purpose

ADD_MONTHS returns the date d plus n months. The argument n can be any integer. If d is the last day of the month or if the resulting month has fewer days than the day component of d, then the result is the last day of the resulting month. Otherwise, the result has the same day component as d.

Examples

select add_months(to_date('2010/01/27','yyyy/mm/dd'),1) from dual

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

2010-2-27

 

select add_months(to_date('2010/01/28','yyyy/mm/dd'),1) from dual

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

2010-2-28

 

select add_months(to_date('2010/01/29','yyyy/mm/dd'),1) from dual

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

2010-2-28

 

select add_months(to_date('2010/01/31','yyyy/mm/dd'),1) from dual

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

2010-2-28

 

LAST_DAY

Purpose

LAST_DAY returns the date of the last day of the month that contains date.

Examples

select last_day(to_date('2010/01/15','yyyy/mm/dd')) from dual

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

2010-1-31

 

 

select last_day(to_date('2010/01','yyyy/mm')) from dual

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

2010-1-31

 

MONTHS_BETWEEN

Purpose

MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.

Examples

SELECT MONTHS_BETWEEN(TO_DATE('2010/05/29','yyyy/mm/dd'),TO_DATE('2010/04/30','yyyy/mm/dd') ) from dual

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

0.967741935483871

 

SELECT MONTHS_BETWEEN(TO_DATE('2010/05/30','yyyy/mm/dd'),TO_DATE('2010/04/30','yyyy/mm/dd') ) from dual

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

1

 

SELECT MONTHS_BETWEEN(TO_DATE('2010/05/31','yyyy/mm/dd'),TO_DATE('2010/04/30','yyyy/mm/dd') ) from dual

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

1

 

SYSDATE

Purpose

SYSDATE returns the current date and time. The datatype of the returned value is DATE. The function requires no arguments. In distributed SQL statements, this function returns the date and time on your local database. You cannot use this function in the condition of a CHECK constraint.

Examples

SELECT sysdate FROM DUAL;

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

2010-10-12 12:02:42

SYSTIMESTAMP

Purpose

SYSTIMESTAMP returns the system date, including fractional seconds and time zone of the system on which the database resides. The return type is TIMESTAMP WITH TIME ZONE.

Examples

SELECT systimestamp FROM DUAL;

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

12-OCT-10 11.59.58.650217 AM +08:00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值