oracle time related functions

oracle database

1. Get the current time

select sysdate() from dual;

2. Add or subtract a certain time interval

select sysdate+ 1/24 from dual;

sysdate+ 1 means adding one day to the time, 1/24 means adding one hour to the time;

select sysdate, add_months(sysdate,4) from dual;--Add 4 months

select sysdate, add_months(sysdate,12*4) from dual;--Add 4 years

To subtract the current time, just add '-' before the number.

DM7 and SQL Server can also use the dateadd(datepart,number,date) function to add and subtract time

Subtract 7 minutes from the current time

select sysdate,sysdate - interval '7' minute from dual;

Subtract 7 hours from the current time

select sysdate - interval '7' hour from dual;

Subtract 7 days from the current time

select sysdate - interval '7' day from dual;

The current time minus the time in July

select sysdate,sysdate - interval '7' month from dual;

Current time minus 7 years

select sysdate,sysdate - interval '7' year from dual;

time interval multiplied by a number

select sysdate,sysdate - 8*interval '7' hour from dual;

3. Date subtraction

Note: The oracle time difference is in days, so it is converted into years, months, and days

Get the number of days between two dates

select floor(sysdate - to_date(‘20220405’,‘yyyymmdd’)) from dual;

  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   //时间差-秒

month difference

 a_date between to_date('20211201','yyyymmdd') and to_date('20211231','yyyymmdd')    

 
4. Date and character conversion method (to_date, to_char)

to_date() converts a date of type str to a date of type time

to_char() converts a date of type time to a date of type str

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //Convert the date into a string   
select to_char(sysdate,'yyyy') as nowYear from dual; //Get the year of the time   
select to_char(sysdate,'mm') as nowMonth from dual; //Get the month of time   
select to_char(sysdate,'dd') as nowDay from dual; //Get the day of time   
select to_char(sysdate,'hh24') as nowHour from dual; //Get time   
select to_char(sysdate,'mi') as nowMinute from dual; //Get time minute   
select to_char(sysdate,'ss') as nowSecond from dual; //Get time second
    
select to_date('2022-05-08 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual //
 

5. Convert int time type to datetime type

Convert timestamp to date format

SELECT TO_CHAR(the column of timestamp/ (1000 * 60 * 60 * 24) + TO_DATE('1970-01-01 08:00:00', 'YYYY-MM-DD HH:MI:SS'), 'YYYY -MM-DD HH:MI:SS') AS CDATE FROM table name; 

select  to_char(1112070645 / ( 60 * 60 * 24) +
       to_date('1970-01-01 08:00:00', 'YYYY-MM-DD HH:MI:SS'), 'YYYY-MM-DD HH:MI:SS')  as cdate  from dual;
 

6. Time comparison

select   dkgl_zb , dkdl , dkdl_zb , hm , hm_zb , mn , mn_zb , lzym , lzym_zb , haomrq , dqsj 
from stati_rlgl_pmcsrb_zh where 1=1 and '2022-04-24' = to_char(haomrq,'yyyy-mm-dd') order by dcno asc;

select   dkgl_zb , dkdl , dkdl_zb , hm , hm_zb , mn , mn_zb , lzym , lzym_zb , haomrq , dqsj 
from stati_rlgl_pmcsrb_zh where 1=1 and haomrq= to_date('2022-04-24 09:12:23','yyyy-mm-dd hh24:mi:ss') order by dcno asc;

7. Other time functions

trunc[truncate to the nearest date, the unit is day], the returned date type

select sysdate S1,                     
     trunc(sysdate) S2, //returns 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 of the current month, without hours, minutes and seconds
     trunc(sysdate,'day') DAY //returns Sunday of the current week, without hours, minutes and seconds
   from dual;

Get the day of the year when the current time is:

select  to_char(sysdate,'DDD'),sysdate from dual;

Find the number of days in the year:

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

5 second intervals:

  select  to_date(floor(to_char(sysdate,'SSSSS')/300) * 300,'SSSSS') ,to_char(sysdate,'SSSSS')     from dual; 


Format output:

select extract(DAY from interval)||'天'||extract(HOUR from interval)||'小时'||extract(MINUTE from interval)||'分钟'||extract(SECOND from interval)||'秒' 间隔 from (  
select numtodsinterval(to_date('2008-10-09 12:30:18','yyyy-mm-dd hh24:mi:ss  
')-to_date('2008-10-28','yyyy-mm-dd'),'DAY') interval from dual)

round rounds to the nearest date
select sysdate S1,
   round(sysdate) S2 ,
   round(sysdate,'year') YEAR,
   round(sysdate,'month') MONTH ,
   round(sysdate,'day') DAY from dual;

Guess you like

Origin blog.csdn.net/chunzhi128/article/details/124649749