本文介绍了获取 SQL 中两个日期之间的周末数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要以函数的形式获取 sql 中日期之间的周末数.我试过了,但在逻辑中的某个地方卡住了.

I need to get the number of weekends between dates in sql as a function. I have tried but stuck up somewhere in the logic.

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

推荐答案

尝试用这个替换 if 语句:

Try replacing the if statement with this:

If ((datepart(dw, @dFrom) = 1) OR (datepart(dw, @dFrom) = 7))

您还应该检查周末以获取结果.

You should also check the end of the week to get the result.

这篇关于获取 SQL 中两个日期之间的周末数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 08:27