Oracle當前日期前後推算公式

2021-07-25 14:15:29 字數 3036 閱讀 5223

平時在專案中會經常遇到需要用某個日期(如當前系統時間)來獲取該日期前後的特定日期,下面列出一些常用的公式。

✳ sysdate為系統當前日期

基礎函式

select to_date('2016-06-15','yyyy-mm-dd') from dual -- 2016-6-15
select to_char(sysdate,'yyyy-mm-dd') from dual -- 系統當前日期
拓展公式

select trunc(to_date('2016-06-15','yyyy-mm-dd'),'year') from dual -- 2016-1-1
select add_months(trunc(to_date('2016-06-15','yyyy-mm-dd'),'year'),12)-1

from dual -- 2016-12-31

select

add_months(last_day(to_date('2016-06-15','yyyy-mm-dd'))+1,-2) from dual -- 2016-5-1

select

add_months(last_day(to_date('2016-01-15','yyyy-mm-dd'))+1,-2) from dual -- 2015-12-1

select

add_months(last_day(to_date('2016-06-15','yyyy-mm-dd')),-1) from dual -- 2016-5-31

select

add_months(last_day(to_date('2016-01-15','yyyy-mm-dd')),-1) from dual -- 2015-12-31

select

trunc(to_date(add_months(last_day(to_date('2016-06-15','yyyy-mm-dd')),-1)),'year') from dual -- 2016-1-1

select

trunc(to_date(add_months(last_day(to_date('2016-01-15','yyyy-mm-dd')),-1)),'year') from dual -- 2015-1-1

select

add_months(trunc(to_date(add_months(last_day(to_date('2016-06-15','yyyy-mm-dd')),-1)),'year'),12)-1 from dual -- 2016-12-31

select

add_months(trunc(to_date(add_months(last_day(to_date('2016-01-15','yyyy-mm-dd')),-1)),'year'),12)-1 from dual -- 2015-12-31

select add_months(trunc(to_date('2016-06-15','yyyy-mm-dd'),'year'),-12) from dual -- 2015-1-1
select trunc(to_date('2016-06-15','yyyy-mm-dd'),'year')-1

from dual -- 2015-12-31

select

add_months(last_day(to_date('2016-06-15','yyyy-mm-dd'))+1,0) from dual -- 2016-7-1

select

add_months(last_day(to_date('2015-12-15','yyyy-mm-dd'))+1,0) from dual -- 2016-1-1

select

add_months(last_day(to_date('2016-06-15','yyyy-mm-dd')),1) from dual -- 2016-7-31

select

add_months(last_day(to_date('2015-12-15','yyyy-mm-dd')),1) from dual -- 2016-1-31

select

trunc(to_date(add_months(last_day(to_date('2016-06-15','yyyy-mm-dd')),1)),'year') from dual -- 2016-1-1

select

trunc(to_date(add_months(last_day(to_date('2015-12-15','yyyy-mm-dd')),1)),'year') from dual -- 2016-1-1

select

add_months(trunc(to_date(add_months(last_day(to_date('2016-06-15','yyyy-mm-dd')),1)),'year'),12)-1 from dual -- 2016-12-31

select

add_months(trunc(to_date(add_months(last_day(to_date('2015-12-15','yyyy-mm-dd')),1)),'year'),12)-1 from dual -- 2016-12-31

select add_months(trunc(to_date('2016-06-15','yyyy-mm-dd'),'year'),12) from dual -- 2017-1-1
select add_months(trunc(to_date('2016-06-15','yyyy-mm-dd'),'year')-1,24) from dual -- 2017-12-31

oracle怎麼使用當前日期

我們有時需要在sql語句中直接獲取當前日期,在oracle資料庫中就存在乙個函式可以直接獲取當前時間。select to char railway xun plan.id id,to char railway xun plan.month id month id,railway xun plan.o...

js獲取當前日期的前後N天日期

function getdatestr adddaycount var dd new date dd.setdate dd.getdate adddaycount 獲取adddaycount天後的日期 var y dd.getfullyear var m dd.getmonth 1 10?0 dd....

Oracle 獲取當前日期及日期格式

oracle 獲取當前日期及日期格式 獲取系統日期 sysdate 格式化日期 to char sysdate yy mm dd hh24 mi ss 或 to date sysdate yy mm dd hh24 mi ss 格式化數字 to number 注 to char 把日期或數字轉換為字...