ORACLE 날짜 시간 함수 대전

62730 단어 Oacle
본문
ORACLE         

   TO_DATE  (   :2007-11-02   13:45:25  )
   
        Year:      
        yy two digits                       :07
        yyy three digits                       :007
        yyyy four digits                       :2007
            
        Month:      
        mm    number                         :11
        mon    abbreviated                   :11 ,     ,  nov     
        month spelled out                   :11 ,     ,  november 
          
        Day:      
        dd    number                         :02
        ddd    number                         :02
        dy    abbreviated               :   ,     ,  fri
        day    spelled out                 :   ,     ,  friday        
        ddspth spelled out, ordinal twelfth 
             
              Hour:
              hh    two digits 12                   :01
              hh24 two digits 24                   :13
              
              Minute:
              mi    two digits 60                     :45
              
              Second:
              ss    two digits 60                     :25
              
                
              Q     digit                                :4
              WW    digit                             :44
              W    digit                              :1
              
        24          : 0:00:00 - 23:59:59....      
        12          : 1:00:00 - 12:59:59 .... 
            
1.            (to_date,to_char)
         
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;   //           
select to_char(sysdate,'yyyy') as nowYear   from dual;   //         
select to_char(sysdate,'mm')    as nowMonth from dual;   //         
select to_char(sysdate,'dd')    as nowDay    from dual;   //         
select to_char(sysdate,'hh24') as nowHour   from dual;   //         
select to_char(sysdate,'mi')    as nowMinute from dual;   //         
select to_char(sysdate,'ss')    as nowSecond from dual;   //       

    
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')    from dual// 

2.      
    select to_char( to_date(222,'J'),'Jsp') from dual      
    
      Two Hundred Twenty-Two     

3.             
   select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;      
            
   select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;      
   monday      
               
   ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';      
              
   TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')     

4.               
    select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;     

5.    null         
   select id, active_date from table1      
   UNION      
   select 1, TO_DATE(null) from dual;      
   
       TO_DATE(null)     

6.      
   a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')      
     12 31   12    12 1  12               。      
     ,          ,  to_char      
      
7.               
               ORACLE      ,   : US7ASCII, date       : '01-Jan-01'      
    alter system set NLS_DATE_LANGUAGE = American      
    alter session set NLS_DATE_LANGUAGE = American      
       to_date        
    select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;      
            NLS_DATE_LANGUAGE,      ,      
             
    select * from nls_session_parameters      
    select * from V$NLS_PARAMETERS     

8.      
   select count(*)      
   from ( select rownum-1 rnum      
       from all_objects      
       where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-      
       02-01','yyyy-mm-dd')+1      
      )      
   where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )      
        not in ( '1', '7' )      
   
     2002-02-28 2002-02-01                
          DBMS_UTILITY.GET_TIME,        (    1/100 ,      ).     

9.          
    select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;      
    1      
   select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;      
    1.03225806451613 
       
10. Next_day         
    Next_day(date, day)      
    
    Monday-Sunday, for format code DAY      
    Mon-Sun, for format code DY      
    1-7, for format code D     

11      
   select to_char(sysdate,'hh:mi:ss') TIME from all_objects      
     :      TIME                
                        
   create or replace function sys_date return date is      
   begin      
   return sysdate;      
   end;      
   
   select to_char(sys_date,'hh:mi:ss') from all_objects;   
     
12.           
     extract()            
    SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer      
    SQL> select sysdate ,to_char(sysdate,'hh') from dual;      
    
    SYSDATE TO_CHAR(SYSDATE,'HH')      
    -------------------- ---------------------      
    2003-10-13 19:35:21 07      
    
    SQL> select sysdate ,to_char(sysdate,'hh24') from dual;      
    
    SYSDATE TO_CHAR(SYSDATE,'HH24')      
    -------------------- -----------------------      
    2003-10-13 19:35:21 19     

       
13.            
   select older_date,      
       newer_date,      
       years,      
       months,      
       abs(      
        trunc(      
         newer_date-      
         add_months( older_date,years*12+months )      
        )      
       ) days 
       
   from ( select      
        trunc(months_between( newer_date, older_date )/12) YEARS,      
        mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,      
        newer_date,      
        older_date      
        from ( 
              select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date      
              from emp 
             )      
      )     

14.                 
   select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual     

16.             
   select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual     

                
   to_char( last_day( to_date('02'    | | :year,'mmyyyy') ), 'dd' )      
      28          

17.yyyy rrrr         
   'YYYY99 TO_C      
   ------- ----      
   yyyy 99 0099      
   rrrr 99 1999      
   yyyy 01 0001      
   rrrr 01 2001     

18.             
   select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate      
   from dual;     

19.5            
   Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')      
   from dual     

   2002-11-1 9:55:00 35786      
   SSSSS  5        

20.            
   select TO_CHAR(SYSDATE,'DDD'),sysdate from dual
        
   310 2002-11-6 10:03:51     

21.    , , ,        
    select      
     Days,      
     A,      
     TRUNC(A*24) Hours,      
     TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,      
     TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,      
     TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds      
    from      
    (      
     select      
     trunc(sysdate) Days,      
     sysdate - trunc(sysdate) A      
     from dual      
   )     


   select * from tabname      
   order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');      
   
   //      
   floor((date2-date1) /365)          
   floor((date2-date1, 365) /30)          
   d(mod(date2-date1, 365), 30)   . 

23.next_day                 ,day 1-7    -   ,1     
   next_day(sysdate,6)            。              。      
   1 2 3 4 5 6 7      
                    
   
   --------------------------------------------------------------- 
   
   select    (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from ddual
                  ss
     
24,round[         ](day:          )
   select sysdate S1,
   round(sysdate) S2 ,
   round(sysdate,'year') YEAR,
   round(sysdate,'month') MONTH ,
   round(sysdate,'day') DAY from dual 

25,trunc[         ,    ] ,        
   select sysdate S1,                     
     trunc(sysdate) S2,                 //      ,    
     trunc(sysdate,'year') YEAR,        //      1 1 ,    
     trunc(sysdate,'month') MONTH ,     //      1 ,    
     trunc(sysdate,'day') DAY           //          ,    
   from dual 

26,           
   select greatest('01-1 -04','04-1 -04','10-2 -04') from dual 

27.     
      :oracle          ,       , 
     
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual        //   - 
      select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual        //   - 
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual             //   - 
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual         //   - 
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual    //   - 
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //   -  

28.    
      :oracle           ,     n,       , 
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual        //    - 
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from dual                                 //    - 
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual            //    - 
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual         //    - 
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual      //    - 
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual   //    -  

29.       ,    
     SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
       Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
       Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
       LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
   FROM dual; 


 .     (            ) 

1,     
   select substr('abcdef',1,3) from dual 

2,      
   select instr('abcfdgfdhd','fd') from dual 

3,     
   select 'HELLO'||'hello world' from dual; 

4, 1)         
    select ltrim(' abc') s1,
    rtrim('zhang ') s2,
    trim(' zhang ') s3 from dual
   2)       
    select trim(leading 9 from 9998767999) s1,
    trim(trailing 9 from 9998767999) s2,
    trim(9 from 9998767999) s3 from dual;
   
5,         Ascii 
   select ascii('a') from dual 

6,  ascii      
   select chr(97) from dual 

7,        
   select length('abcdef') from dual 

8,initcap(      ) ,lower(   ),upper(   )
   select lower('ABC') s1, 
       upper('def') s2, 
       initcap('efg') s3
   from dual; 

9,Replace
   select replace('abc','b','xy') from dual; 

10,translate
   select translate('abc','b','xx') from dual; -- x 1  

11,lpad [   ] rpad [   ](        )
   select lpad('func',15,'=') s1, rpad('func',15,'-') s2 from dual;
   select lpad(dname,14,'=') from dept; 

12, decode[  if ..then   ]    :       ,                
   select deptno,decode(deptno,10,'1',20,'2',30,'3','  ') from dept;
    :
   select seed,account_name,decode(seed,111,1000,200,2000,0) from t_userInfo//  seed 111,  1000; 200, 2000;   0
   select seed,account_name,decode(sign(seed-111),1,'big seed',-1,'little seed','equal seed') from t_userInfo//  seed>111,    ; 200,    ;     

    

13 case[  switch ..case   ]
    SELECT CASE X-FIELD 
         WHEN X-FIELD < 40 THEN 'X-FIELD    40'
         WHEN X-FIELD < 50 THEN 'X-FIELD    50'
         WHEN X-FIELD < 60 THEN 'X-FIELD    60'
         ELSE 'UNBEKNOWN'
        END
   FROM DUAL 
   
    :CASE                。            , Decode    。 

 .    
1,    (ceil     ,floor     )
   select ceil(66.6) N1,floor(66.6) N2 from dual; 

2,   (power)       (sqrt)
   select power(3,2) N1,sqrt(9) N2 from dual; 

3,  
   select mod(9,5) from dual; 

4,         (round:    ,trunc:    )
   select round(66.667,2) N1,trunc(66.667,2) N2 from dual; 

5,      (     1,   -1)
   select sign(-32),sign(293) from dual; 

 .    
1,to_char()[               ]
   1) select to_char(sysdate) s1,
        to_char(sysdate,'yyyy-mm-dd') s2,
        to_char(sysdate,'yyyy') s3,
        to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,
        to_char(sysdate, 'hh24:mi:ss') s5,
        to_char(sysdate,'DAY') s6 
    from dual;
   2) select sal,to_char(sal,'99999') n1,to_char(sal,'99,999') n2 from emp 

2, to_date()[            ] 
    insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));
   
3, to_number()         
    select to_number(to_char(sysdate,'hh12')) from dual; //         
   
 .    
   1.user: 
              
    select user from dual;
    
   2.vsize: 
               
    select vsize('HELLO') from dual;
   
   3.nvl(ex1,ex2):  
    ex1      ex2,        ex1(  ) 
     :        ,   0,       
    select comm,nvl(comm,0) from emp;
   
   4.nullif(ex1,ex2): 
         ,        
     :         ,    ,      
    select nullif(sal,comm),sal,comm from emp;
   
   5.coalesce:  
                 
    select comm,sal,coalesce(comm,sal,sal*10) from emp;
   
   6.nvl2(ex1,ex2,ex3) :
      ex1   ,  ex2,    ex3
     :                  
      select nvl2(comm,ename,') as HaveCommName,comm from emp;
   
   
 .    
max min avg count sum
11)    30      ,    ,    ,   ,      ,         
     select max(ename),max(sal), 
     min(ename),min(sal),
     avg(sal),
     count(*) ,count(job),count(distinct(job)) ,
     sum(sal) from emp where deptno=30;
2,  group by   having    
   1)          ,    ,   ,      ,         
    select deptno, max(ename),max(sal),
    min(ename),min(sal),
    avg(sal),
    count(*) ,count(job),count(distinct(job)) ,
    sum(sal) from emp group by deptno;
   
   2)  30     ,    ,   ,      ,          
    select deptno, max(ename),max(sal),
    min(ename),min(sal),
    avg(sal),
    count(*) ,count(job),count(distinct(job)) ,
    sum(sal) from emp group by deptno having deptno=30;
   
3, stddev           
    select deptno,stddev(sal) from emp group by deptno;
    variance          
    select deptno,variance(sal) from emp group by deptno; 

4,   rollup cube    Group By
    rollup                   
    cube                   
    select deptno,job ,sum(sal) from emp group by deptno,job;
    select deptno,job ,sum(sal) from emp group by rollup(deptno,job); 
    cube                 
    select deptno,job ,sum(sal) from emp group by cube(deptno,job); 

 、   
                     .
            ,       
   create global temporary table temp_dept
   (dno number,
   dname varchar2(10))
   on commit delete rows;
   insert into temp_dept values(10,'ABC');
   commit;
   select * from temp_dept; --     ,      
   on commit preserve rows:            (    )
   on commit delete rows:        (              ) 

 

좋은 웹페이지 즐겨찾기