2014-09-18 94 views
2

如何找出一個月中的星期日?如何從一個月中的任何一天中減去星期日

請幫我在這..

沒有星期天:4當月,我需要減去天數,這些在一個月一個月-sundays

::天

如果我通過之間的日期和我需要得到該期間的週日計數..

非常感謝您的幫助。

Sunitha ..

+0

''TO_CHAR''天'格式掩碼'會給你一天,使用'CASE'來檢查它是否是'SUNDAY'並且不包含它。有關更詳細的答案,請提供一個「測試用例」。 – 2014-09-18 16:02:02

回答

0

試試這個:

select to_char(last_day(sysdate),'dd') - 
    ((next_day(last_day(trunc(sysdate)),'sunday')-7 
    -next_day(trunc(sysdate,'mm')-1,'sunday'))/7+1) as result 
    from dual 

爲一個週期,你可以做到這一點之間計數日期:

SELECT (next_day(TRUNC(TO_DATE('28/09/2014','DD/MM/YYYY')),'sunday') - 
    next_day((trunc(TO_DATE('13/09/2014','DD/MM/YYYY'))),'sunday')-7)/7+1 AS RESULT 
    FROM DUAL 
+0

對不起,我把30放在一個不好的地方 – Aramillo 2014-09-18 18:19:19

+0

謝謝你的rply。 。如果我通過之間的日期和我需要得到該期間的星期日計數.. – Sunitha 2014-09-19 03:31:33

+0

我更新我的文章,我希望這有助於:) – Aramillo 2014-09-19 15:48:50

-1
SELECT (
     (
      TO_CHAR(next_day(last_day(TRUNC(sysdate))-7,'SUN'),'DD')- 
      TO_CHAR(next_day(last_day(TRUNC(sysdate-30)),'SUN'),'DD') 
     )/7 
     )+1 
FROM dual; 
+0

謝謝你的回覆..如果我通過之間的日期和我需要得到該期間的星期日數。請幫助 – Sunitha 2014-09-19 03:30:23

0

如果從和日期有,您應該首先使用分層查詢生成它們之間的所有日期。然後從他們只過濾星期日。

select count(1) 
from (
    select start_date + level - 1  as dates 
    from dual 
    connect by start_date + level - 1 <= end_date 
) 
where to_char(dates,'fmday') = 'sunday'; 

如果要計算非週日的數量當月,發現每月的第一天和最後一天,按照如上相同的程序。

select count(1) 
from (
    select start_date + level - 1 as dates 
    from (
     select trunc(sysdate,'month') as start_date, 
       last_day(sysdate) as end_date 
     from dual 
     ) 
    connect by start_date + level - 1 <= end_date 
) 
where to_char(dates,'fmday') != 'sunday'; 

Sqlfiddle

相關問題