Oracle SQL

2007-11-15 6:53 am
想問o下點樣寫句sql 出o黎係可以計到每個月有幾多個星期日呢?

e.g. 由 1/11/2007 至 30/11/2007
total: 有4個星期日

我想搵番4依個數出o黎!!

thanks!

回答 (1)

2007-11-15 10:35 am
✔ 最佳答案
Use NEXT_DAY function, returns the first weekday from the date specified.
NEXT_DAY(date, weekday)

SELECT NEXT_DAY( TO_DATE ('11/01/2007', 'MM/DD/YYYY'), 'SUNDAY')
FROM DUAL
returns "4-Nov-2007"

SELECT (TO_DATE ('11/30/2007', 'MM/DD/YYYY')
- NEXT_DAY( TO_DATE ('11/01/2007', 'MM/DD/YYYY'), 'SUNDAY') + 1) /7
FROM DUAL

If the result is an integer, that is the answer. If the result is a decimal number, round up to the next integer.

For example, find the Sunday between 5-Nov-2007 and 24-Nov-2007. Next_Day function returns 11-Nov-2007. Between 11-Nov and 24-Nov are 14 days (24 - 11 +1). 14 / 7 = 2 Sundays

To Find the Sundays between 3-Nov-2007 and 19-Nov-2007. Next_Day function returns 4-Nov-2007. Between 4-nov and 19-Nov are 16 days (19 - 4 +1). 16 / 7 = 2.3 = 3 Sundays


收錄日期: 2021-04-25 20:33:30
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071114000051KK04452

檢視 Wayback Machine 備份