Oracle日期比較

2021-09-30 14:48:14 字數 1701 閱讀 2316

系統時間就是發部落格的時間,即2012-8-5。

1 日期比較

select * from dual where sysdate > to_date('2000.8.5','yyyy.mm.dd')

select * from dual where to_date('2012.8.5','yyyy.mm.dd')> to_date('2012.6.5,'yyyy.mm.dd')

select * from dual where to_date('2012.8.5','yyyy.mm.dd') = to_date('2012.8.5','yyyy.mm.dd')

2 日期相減

select (sysdate - to_date('2013.3.1','yyyy.mm.dd')) from dual

結果 -207.628275462963

結果是相差的天數,相差的小時分鐘秒也被轉換為天數

3 日期間距

select * from dual where sysdate

between to_date('2008-08-05 00:00:00','yyyy-mm-dd hh24:mi:ss')

and to_date('2013-08-05 00:00:00','yyyy-mm-dd hh24:mi:ss')

4 其他應用

獲取昨天

select trunc(sysdate-1) from dual;

2012-8-4

檢查一下

select to_char (trunc(sysdate-1),'yyyy-mm-dd hh24:mi:ss') from dual;

結果 2012-08-04 00:00:00

獲取上個月第一天00:00:00

select add_months(trunc(sysdate,'mon'),-1) from dual

select add_months(trunc(sysdate,'mon'),-13) from dual

結果 2012-7-1

獲取上個月今天00:00:00

select trunc(add_months(sysdate,-1)) from dual

結果 2012-7-5

獲得本季度第一天

select trunc(add_months(sysdate,0),'q') from dual

結果 2012-7-1

獲得上季度第一天

select trunc(add_months(sysdate,-3),'q') from dual

結果 2012-4-1

獲得去年1月1日

to_char(add_months(trunc(sysdate, 'year'), -12), 'yyyy-mm-dd')

結果 2011-01-01

相減

to_date('20110105','yyyymmdd')-7 此處7代表天

結果 2010-12-29

oracle日期時間比較

select cicai as merchantname,tre.bank code as bankcode,sum tr.trade sum as totalmount,count as count,tr.biz type as biztype from trade relation tre,tr...

mysql與oracle 比較日期

select from cot gw main where create date to date 20150105 yyyymmdd 如上語句可能想查詢出建立日期在2015年1月5日那天或之前的,但實際上因為你的create date是date屬性的 當你將條件設定為2015年01月05號,而不設...

Oracle的日期比較以及處理

1 日期比較時精確到日,可以使用 trunc sysdate,dd 函式。函式支援格式有 yyyy mm dd hh mi 可以用 select trunc sysdate,yyyy from dual 測試看看 返回 日期 select sysdate from dual 當時日期 select ...