Common date processing of oeacl

Date processing common functions

  1. sysdate: current date and time;
  2. to_char(): convert numeric or date type to character type;
  3. to_date(): date conversion function, for example, to_date ('2017-09-27 10:00:00 ',' yyyy MM DD hh24: Mi: Ss');
  4. last_day(sysdate): returns the last day of the month in which the date is located;
  5. TRUNC (number [, decisions]): specify the element format to truncate part of the date value;
  6. add_months(D,N): returns the corresponding date and time after D plus N months. When n is positive, it means after D; when n is negative, it means before D; when n is decimal, it will automatically delete the decimal part first, and use the integer part;
  7. Decode (condition, value 1, translation value 1, value 2, translation value 2 Value n, translation value n, default value): returns the corresponding value according to the condition;
  8. ceil(n): take the minimum integer greater than or equal to the value n;
  9. floor(n): take the maximum integer less than or equal to the value n;
  10. dbtimezone(): returns the time zone;
  11. ......

Common operations

--Get the current date
select sysdate from dual;

--Get the current date is the week of the month
select to_char(sysdate,'YYYYMMDD HH24:MI:SS W') from dual;

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

--Get the current date is the day of the week,Notice that Sunday is the first day back number 1 Start Sunday
select to_char(sysdate,'d') from dual;
select to_char(sysdate,'yyyy') from dual;--Year of acquisition
select to_char(sysdate,'q') from dual;--Get quarter
select to_char(sysdate,'mm') from dual;--Month of acquisition
select to_char(sysdate,'dd') from dual;--Date of acquisition
select to_char(sysdate,'ddd') from dual;-- Day of the year
select to_char(sysdate,'ww') from dual; -- Week of the year
select to_char(sysdate,'w') from dual;  -- Information of the month
select to_char(sysdate,'day') from dual; --What day is today?
select to_char(sysdate,'hh') from dual; --Hour 12 hour system
select to_char(sysdate,'hh24') from dual; --24-hour system
select to_char(sysdate,'MI') from dual; --branch
select to_char(sysdate,'ss') from dual; --second

--Get the date of 0:00:00 in the morning
select trunc(sysdate) from dual;

--Get the last second of the day
select trunc(sysdate)+0.99999 from dual;

--Get the specific value of the hour
select trunc(sysdate)+1/24 from dual;
select trunc(sysdate)+7/24 from dual;

--Get a day tomorrow at 0:00:00 a.m
select trunc(sysdate+1) from dual;
select trunc(sysdate)+1 from dual;

--Date of the first day of this month
select trunc(sysdate,'mm') from dual;

--Date of the first day of the next month
select trunc(add_months(sysdate, 1),'mm') from dual;

--Return to the last day of the current month
select last_day(sysdate) from dual;
select last_day(trunc(sysdate)) from dual;
select trunc(last_day(sysdate)) from dual;
select trunc(add_months(sysdate,1),'mm') - 1 from dual;

--Get every day of the year
select trunc(sysdate,'yyyy')+ rn -1 date0 from
(select rownum rn from all_objects
where rownum<366);

--Add 2 years to the existing date
select add_months(sysdate,24) from dual;

--Judge whether the year of a certain day is a run year
select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','Leap year','Ordinary year') from dual;

--Judge whether it is a run year after two years
select decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','Leap year','Ordinary year') from dual;

--Query quarter of current date
select ceil(to_number(to_char(sysdate,'mm'))/3) from dual;


Calculate the data recorded by the user this week, this day and this month according to the time and user id

-- Today 
select y.create_by,count(y.create_date), from test y,sys_user u
 where to_date(y.create_date)=to_date(sysdate) and
group by y.create_by,;

--This week
select create_by,count(create_date)
from test
where to_date(create_date)>=to_date(sysdate-to_number(to_char(sysdate-1,'D')+1)) and slzt='1'
group by create_by;

--This month
select to_char(create_date,'yyyy-mm'),create_by,count(create_date) from test
where to_date(create_date)>=to_date(trunc(sysdate,'mm')) and to_date(create_date)<=to_date(last_day(sysdate))
group by to_char(create_date,'yyyy-mm'),create_by;

Tags: less

Posted on Fri, 01 May 2020 06:09:19 -0700 by mimintweak