スティーブ・カースの式を使用して、与えられた日付の前月のn番目の週にn日目を取得しています。それは素晴らしい作品です - しかし、私は実際には、モジュラスのオペランドは、数式で何を理解していない。誰か説明できますか?私は、以下のいくつかのギブンスを供給するように試みた:スティーブ・カスのモジュラスの説明月のn番目の週にn番目の曜日を見つける式
declare @svcDate as datetime= '1/6/2017', @myN as tinyint=4, @myD as tinyint=1
declare @ret datetime;
declare @first datetime -- First of the month of interest (no time part)
declare @nth tinyint -- Which of them - 1st, 2nd, etc.
declare @dow tinyint -- Day of week we want - this server is set to - 1 sun, 2 mon, 3 tue, 4 wed, 5 thur, 6 fri, 7 sat
set @first = dateadd(month,-1,datefromparts(year(@svcDate), month(@svcDate), 1)) --first of last month
set @nth = @myN
set @dow = @myD
--Select @first 12/1/2016
set @ret = @first + 7*(@nth-1)
--select @ret 12/22/2016 Thurs
--datepart(weekday,@ret)=5
set @ret= @ret + (7 + @dow - datepart(weekday,@ret))%7
if(@ret IS NULL)
set @ret='1/1/2017';
select @ret
select 3%7 --returns 3
select convert(decimal(18,2),3)/convert(decimal(18,2),7) -- returns 0.42857142857142857142
実際の質問に答えることは忘れました。それはまったく必要ですか? –
@PM: "[モジュラス演算子]は** **の式で**は"モジュラス演算子はどこでも同じことです...整数除算操作の残りの部分を返します。私が答えることを怠った質問は、質問されなかった質問だった... ** "なぜmodulus演算が式に必要なのですか?" '@ dow'と' weekday'の減算が潜在的に負であるため、式に入っていると思われます。 7を足してモジュラス演算を行って1週間を除いた数式は、0から6の範囲の日数を返します。 – spencer7593
ありがとうございます、Spencer7593。それは私には短縮形については意味があり、本当にあなたの非常に詳細な答えに感謝します。概念の周りに小さな灰色の細胞を得ることができました。 – user1795131