Addition and subtraction of Oracle time and date

Source: Internet
Author: User
Date of last month: selectlast_day (add_months (sysdate,-1) fromdual; last second of this month: selecttrunc (add_months (sysdate, 1), mm)-12460fromdual date of this week: selecttrunc (sysdate, day) + 1fromdual days from the beginning to the present

Last month's date: select last_day (add_months (sysdate,-1) from dual;
Last second of the month: select trunc (add_months (sysdate, 1), 'mm')-1/24/60/60 from dual
Date of the week: select trunc (sysdate, 'day') + 1 from dual


Days from the beginning of the year to date: select ceil (sysdate-trunc (sysdate, 'Year') from dual;

Today is the week of this year: select to_char (sysdate, 'fmww') from dual
Today is the week of the month: select to_char (sysdate, 'ww ')-to_char (trunc (sysdate, 'mm'), 'ww ') + 1 as "weekofmon" from dual

Number of days this month
Select to_char (last_day (sysdate), 'dd') days from dual
Number of days this year
Select add_months (trunc (sysdate, 'Year'), 12)-trunc (sysdate, 'Year') from dual
The date of next Monday
Select next_day (sysdate, 'monday') from dual


========================================================== ====

-- Workday Calculation Method

Create table t (s date, e date );
Alter session set nls_date_format = 'yyyy-mm-dd ';
Insert into t values ('2017-03-01 ', '2017-03-03 ');
Insert into t values ('2017-03-02 ', '2017-03-03 ');
Insert into t values ('2017-03-07 ', '2017-03-08 ');
Insert into t values ('2017-03-07 ', '2017-03-09 ');
Insert into t values ('2017-03-05 ', '2017-03-07 ');
Insert into t values ('2017-02-01 ', '2017-03-31 ');

-- Assume that all dates do not contain time; otherwise, add trunc before all dates.
Select s, e, e-s + 1 total_days,
Trunc (e-s + 1)/7) * 5 + length (replace (substr ('000000', to_char (s, 'D '), mod (e-s + 1, 7), '0', '') work_days
From t;

-- Drop table t;

========================================================== ==========================================================

Determine whether the current time is morning afternoon or evening

Select case
When to_number (to_char (sysdate, 'hh24') between 6 and 11 then 'am'
When to_number (to_char (sysdate, 'hh24') between 11 and 17 then 'more'
When to_number (to_char (sysdate, 'hh24') between 17 and 21 then''
End
From dual;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.