我需要将 sql 中日期之间的周末数作为函数获取。我已经尝试过,但在逻辑的某个地方卡住了。
CREATE FUNCTION fnc_NumberOfWeekEnds(@dFrom DATETIME, @dTo DATETIME)
RETURNS INT AS
BEGIN
Declare @weekends int
Set @weekends = 0
While @dFrom <= @dTo Begin
If ((datepart(dw, @dFrom) = 1))
Set @weekends = @weekends + 1
Set @dFrom = DateAdd(d, 1, @dFrom)
End
Return (@weekends)
END