pgsql

Date function

select * from orders where to_date(to_char(payment_time, 'yyyy-MM-dd'),'yyyy-MM-dd')< to_date('2016-02-19','yyyy-MM-dd')


Oracle Medium TO_DATE format
TO_DATE format (take time: 2007-11-02 13:45:25 as an example)
 
        Year:    
        yy two digits year Display value: 07
        yyy three digits three digits year Display value: 007
        yyyy four digits four digits year Display value: 2007
          
        Month:    
        mm number Two-digit month Display value: 11
        mon abbreviated Character set display Display value: November, if English version, display nov   
        month spelled out Character set display Display value: November, if English version, display november
        
        Day:    
        dd number The day of the month Display value: 02
        ddd number Day of the current year Display value: 02
        dy abbreviated Abbreviated day of the week Display value: Friday, if it is in English, display fri
        day spelled out Full day of the week Display value: Friday, if it is in English, display Friday      
        ddspth spelled out, ordinal twelfth
           
              Hour:
              hh two digits 12-hour base Display value: 01
              hh24 two digits 24-hour base Display value: 13
            
              Minute:
              mi two digits 60 base Display value: 45
            
              Second:
              ss two digits 60 base Display value: 25
            
              Other
              Q digit quarter Display value: 4
              WW digit Week of the year Display value: 44
              W digit Week of the month Display value: 1
            
        The time range in 24-hour format is: 0:00:00 - 23:59:59....    
        The time range in 12-hour format is: 1:00:00 - 12:59 :59 ....
          
1. Date and character conversion function usage (to_date,to_char)
       
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; // date converted to string 
select to_char(sysdate,'yyyy') as nowYear from dual; //get the year of the timeselect 
to_char(sysdate,'mm') as nowMonth from dual; //get the month of the timeselect 
to_char(sysdate,'dd') as nowDay from dual; //Get the day of the time 
select to_char(sysdate,'hh24') as nowHour from dual; //Get the time of the dayselect 
to_char(sysdate,'mi') as nowMinute from dual; //Get the minute of the time 
select to_char(sysdate,'ss') as nowSecond from dual; //get time in seconds

  
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    
  
    shows Two Hundred Twenty-Two   

3. Find the day of the week    
   select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;    
   Monday    
   select to_char (to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;    
   monday    
   set the date language    
   ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';    
   also can be    
   TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')   

4.     Number of days between two
    datesselect floor(sysdate - to_date('20020405','yyyymmdd')) from dual;   

5. The usage of time is null    
   select id, active_date from table1    
   UNION    
   select 1, TO_DATE(null) from dual;    
 
   note to use TO_DATE(null)   

6. Month difference 
   a_date between to_date('20011201','yyyymmdd') and to_date( '20011231', 'yyyymmdd')    
   Then after 12:00 noon on December 31 and before 12:00 on December 1 are not included in this range.    
   Therefore, when the time needs to be precise, I think to_char is still necessary.
    
7. Date format conflict    
    The input format depends on the type of ORACLE character set you have installed, such as: US7ASCII, the type of date format is: '01-Jan-01 '    
    alter system set NLS_DATE_LANGUAGE = American    
    alter session set NLS_DATE_LANGUAGE = American    
    or write    
    select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','     in to_date
    Note that I only mentioned NLS_DATE_LANGUAGE, of course there are many more,    
    see    
    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' )    
 
   to find the number of days between 2002-02-28 and 2002-02-01 except Monday and July, and    
   call them before and after respectively DBMS_UTILITY.GET_TIME, let the results be subtracted (you get 1/100th of a second, not a millisecond).   

9. Find the month   
    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        begin        create or replace function sys_date return date is        You can create a function to deal with this problem    
   Note: The TIME of the first record is the same as the last row.    



   return sysdate;    
   end;    
 
   select to_char(sys_date,'hh:mi:ss') from all_objects; 
   
12. Get hours    
     extract() Find the field value of date or interval value
    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. Year-month-day processing    
   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. The way to deal with the indeterminate number of days in a month    
   select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'), last_day(sysdate) from dual   

16. Find out the number of days in this year    
   select add_months(trunc(sysdate,' year'), 12) - trunc(sysdate,'year') from dual   

   leap year processing method    
   to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' )    
   if it is 28, it is not Leap year   

17. The difference between yyyy and    
   rrrr'YYYY99 TO_C    
   ------- ----    
   yyyy 99 0099    
   rrrr 99 1999    
   yyyy 01 0001    
   rrrr 01 2001   

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

19.5 second interval    
   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    
   represents 5-digit seconds Number   

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

21. Calculate hours, minutes, seconds, milliseconds    
    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 ) as year    
   floor((date2-date1, 365) /30) as month    
   d(mod(date2-date1, 365), 30) as day.

23.next_day function returns the date of the next week, day is 1-7 or Sunday-Saturday, 1 means Sunday
   next_day(sysdate, 6) is the next Friday from the current one. The numbers that follow are from Sunday.    
   1 2 3 4 5 6 7th    
   123456  
 
   ------------------------------------ ---------------------------
 
   select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm -dd hh24:mi:ss'))*24*60*60 from ddual
   date returns days and then converts to ss
   
24,round[round to the nearest date](day: round to the nearest Sunday)
   select sysdate S1,
   round(sysdate) S2 ,
   round(sysdate,'year') YEAR,
   round(sysdate,'month' ) MONTH ,
   round(sysdate,'day') DAY from dual

25, trunc[truncated to the nearest date, the unit is day], the return is the date type
   select sysdate S1,                   
     trunc(sysdate) S2, //return the current date , without hours, minutes and seconds
     trunc(sysdate,'year') YEAR, //returns January 1 of the current year, without hours, minutes and seconds
     trunc(sysdate,'month') MONTH, //returns the 1st day of the current month, without hours, minutes and seconds
     trunc(sysdate,'day') DAY //Return the Sunday of the current week, no hours, minutes and seconds
   from dual

26, return the latest date in the date list
   select greatest('01-January-04','04-January-04 ','Oct-Feb-04') from dual

27. Calculation time difference
     Note: The oracle time difference is in days, so it is converted into adult months, days
   
      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 //time difference -Second

28. Update time
     Note: The addition and subtraction of oracle time is based on the number of days, and the amount of change is set to n, so convert the adult month, day
     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 //change time-year
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months (sysdate,n) as newTime from dual //Change time-month
     select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi: ss') as newTime from dual //Change time-day
     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 //change time-hour
     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;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327100547&siteId=291194637