1. 程式人生 > >oracle資料庫按照時間查詢資料

oracle資料庫按照時間查詢資料

資料庫時間型別為timstamp,按照時間進行資料查詢

select * from table1 
where 
TS between TO_DATA(‘2017-7-25 16:30:00’,’yyyy-mm-dd h24:mi:ss’) 
and TO_DATA(‘2017-7-25 19:30:00’,’yyyy-mm-dd h24:mi:ss’

檢視兩個時間差之前大於5秒的資料

select count(*) from 
(select * from table1 
where 
TS between TO_DATA(‘2017-7-25 16:30:00’,’yyyy-mm-dd h24:mi:ss’) 
and TO_DATA(‘2017-7-25 19:30:00’,’yyyy-mm-dd h24:mi:ss’) order by (ORACLE_TS) desc) tb
where extract(second from (tb.ORACLE_TS-tb.TS)) > 5
注:oracle中extract()函式從oracle 9i中引入,用於從一個date或者interval型別中擷取到特定的部分
select extract(year from systimestamp) year    
      ,extract(month from systimestamp) month    
      ,extract(day from systimestamp) day    
      ,extract(minute from systimestamp) minute    
      ,extract(second from systimestamp) second    
      ,extract(timezone_hour from systimestamp) th    
      ,extract(timezone_minute from systimestamp) tm    
      ,extract(timezone_region from systimestamp) tr    
      ,extract(timezone_abbr from systimestamp) ta    
from dual;
結果如下:
     YEAR      MONTH        DAY     MINUTE     SECOND         TH         TM TR         TA    
---------- ---------- ---------- ---------- ---------- ---------- ---------- --------- ----------    
      2011          5         17          7     14.843          8          0 UNKNOWN   UNK