当前位置:数据库 > Oracle >>

oracle日期处理

oracle日期处理
 
-- 求年月日 
select  to_char(sysdate, 'yyyyMMDD'), sysdate from dual ; 
 
-- 求年月日系统的昨天 
select  to_char(sysdate-1, 'yyyyMMDD'), sysdate from dual ; 
 
-- 求年月日系统的前天 
select  to_char(sysdate-2, 'yyyyMMDD'), sysdate from dual ; 
 
 
-- 求年月日系统的明天 
select  to_char(sysdate+1, 'yyyyMMDD'), sysdate from dual ; 
 
 
-- 求年月日系统的后天 
select  to_char(sysdate+2, 'yyyyMMDD'), sysdate from dual ; 
 
-- 求周 
select  to_char(sysdate, 'yyyyww'), sysdate from dual ; -- 201248 
 
--年月(数字) 
select to_number(to_char(sysdate, 'yyyymm')) from dual ; -- 201211 
--年月(字符) 
select to_char(sysdate, 'yyyymm') from dual ; --201211 
--月 (数字) 
select to_number(to_char(sysdate, 'MM')) from dual ; --11 
--月(字符) 
select to_CHAR( sysdate,'MM') from dual ; -- 11 
select to_CHAR( to_date('201201' ,'yyyymm'),'MM') from dual ; -- 01 
select to_CHAR( to_date((select to_number(to_char(sysdate, 'yyyymm')) from dual ) ,'yyyymm'),'MM') from dual ; -- 11 
 
SELECT TO_NUMBER(TO_CHAR(TO_DATE('201301' ,'YYYYMM'),'MM')) FROM DUAL ; 
 
循环日期 
declare 
  -- Local variables here 
  i integer; 
  a varchar2(100); 
begin 
  -- Test statements here 
  i:=0; 
  while i < 31 loop 
    select  to_char(to_date('20130101' ,'yyyyMMDD')+i, 'yyyyMMDD') into a from dual ; 
      dbms_output.put_line(i||' : '||a); 
      i:=i+1; 
  end loop; 
end;
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,