Oracle functions for obtaining the start time and end time of the last ten days

Source: Internet
Author: User

Oracle functions for obtaining the start time and end time of the last ten days

[SQL] -- get create or replace function fd_lastxunstart (rq in date) return string is refstr varchar2 (50); v_rq date; begin -- get date v_rq of the last ten: = trunc (rq); select case decode (trunc (to_char (v_rq, 'dd')-1)/10), 0, 'upgrade', 1, 'mid ', 'late then ') when 'late then -- returns to_char (add_months (v_rq,-1), 'yyymm') of the last month ') | '21' when' mid-term 'then to_char (v_rq, 'yyymmm') | '01' else to_char (v_rq, 'yyymmm ') | '11' end into refstr from dual; return refstr; end fd_lastxunstart; -- the returned result is: select sysdate from dual; select fd_lastxunstart (sysdate) from dual; select fd_lastxunstart (to_date ('201312', 'yyyymmdd') from dual; select fd_lastxunstart (to_date ('20170101', 'yyyymmdd') from dual; select fd_lastxunstart (to_date ('201312', 'yyyymmdd') from dual; -- the execution result is: 12:08:39, 20130821, 20130221, 20130301, 20130311, ---- get the end date of the last ten days -- pass in a value of the date type, returns the create or replace function fd_lastxunend (rq in date) return string is refstr varchar2 (50); v_rq date; begin -- Obtain the date v_rq of the previous day: = trunc (rq); select case decode (trunc (to_char (v_rq, 'dd')-1)/10), 0, 'upgrade', 1, 'mid ', 'late then ') when 'late then -- returns the last day of the previous month -- chr (39). This is a quotation mark to_char (last_day (add_months (v_rq,-1 )) + 1-1/24/60/60, 'yyyymmdd') when' mid-term 'then to_char (v_rq, 'yyyymmm') | '10' else to_char (v_rq, 'yyyymmd ') | '20' end into refstr from dual; return refstr; end fd_lastxunend; -- the obtained result is: select fd_lastxunend (sysdate) from dual; select fd_lastxunend (to_date ('201312', 'yyyymmdd') from dual; select fd_lastxunend (to_date ('20170101', 'yyyymmdd') from dual; select fd_lastxunend (to_date ('201312', 'yyyymmdd') from dual; select fd_lastxunend (to_date ('20170101', 'yyyymmdd') from dual; -- execution result: 20130831, 20130228, 20130310, 20130310, 20130220 -- observe the effect of 1/24/60/60. This is a second select last_day (add_months (trunc (sysdate),-1 )) + 1-1/24/60/60 from dual; select last_day (add_months (trunc (sysdate),-1) from dual; select last_day (add_months (trunc (sysdate),-1 )) + 1 from dual; -- execution result: 23:59:59

 


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.