简体   繁体   中英

get date of 3rd friday in the month with SQL without using level function

使用SQL获取月份中的第三个星期五的日期(不使用级别功能),还有一个月的工作日和所有月份的第三个星期五,谢谢

You can try below code.

SELECT NEXT_DAY(TO_DATE('JAN-2016','MON-YYYY')+13, 'FRIDAY') THIRD_FRIDAY FROM dual;

13 : Is to use 3rd week (addition of 13 days)

Here's something that will get you the 3rd Friday in the month for all months in 2016. I'm not sure why you want to avoid using connect by and level, though.

with months as (select to_date('01/01/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/02/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/03/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/04/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/05/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/06/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/07/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/08/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/09/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/10/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/11/2016', 'dd/mm/yyyy') start_of_month from dual union all
                select to_date('01/12/2016', 'dd/mm/yyyy') start_of_month from dual)
select start_of_month,
       next_day(start_of_month - 1, 'FRIDAY') + 14 third_friday_in_month
from   months;

As for your requirement to get the number of business days in a month, it depends on what you mean by "business days". Do you mean weekdays? Or weekdays minus public holidays? Something else?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM