2

2つの日付の間の日曜日の数を計算する数式がExcelにありますが、SQLでも同じことをしたいのですが、その方法がわかりません。手伝ってくれますか?

=SUM(INT((WEEKDAY(A1-8,1)+B1-A1)/7))

Declare @From as Datetime 
Declare @To as Datetime 

Set @From = '7 Jan 2013' 
Set @To = '18 Mar 2013' 

SELECT SUM(WEEKDATE(@From-8,1)+ @End-@From)/7)) AS No_Of_Sundays 
FROM TIME

ありがとう

ウェイン

4

2 に答える 2

0

sql-server 2005 以降を使用している場合、これはCTE. ここにデモがあります

declare @From date = '20130107' --'7 Jan 2013' 
declare @To date = '20130318'  --'18 Mar 2013' 

;with CTE as (
  select 0 number , datename(weekday, @from) dName
  union all
  select number+1, datename(weekday,dateadd(day,number+1,@from)) dName
  from CTE 
  where dateadd(day, number + 1,@from) <= @to and
       datename(weekday,dateadd(day,number,@from)) <> 'sunday'
)

select top(1) (datediff(day,@from,@to) - number )/7 + 
               case when number = 0 and dname <> 'sunday' then 0 else 1 end
               sundays
from CTE
order by number desc
于 2013-02-28T10:36:42.977 に答える