2017-04-04 123 views

回答

1

您可以使用DATENAME检查'Friday'做到这一点,是这样的:

DECLARE @startDate DATETIME 
DECLARE @endDate DATETIME 
SET @startDate = '2017-04-04' 
SET @endDate = '2017-04-12' 

Declare @count int 
set @count=0 

    while @startDate < @endDate 
    Begin 
     IF DATENAME(dw, @startDate) <> 'Friday' 
      SET @count = @count + 1 
     SET @startDate = DateAdd(d, 1, @startDate) 
    END 
select @count 

这将导致:

7 
+0

它的未来8.什么应该根据你的答案? –

+0

它应该是7,因为他们之间有一个星期五..用它来检查.... SELECT DATEDIFF(day,'2017-04-04','2017-04-12')AS DiffDate..This is returned 8 ..你的代码应该返回7 .. – TanvirArjel

+0

datediff检查你需要在任一方向跨越达到日期的边界。 –

0

略低于条款添加到您的查询

select count(*) from table 
where startDate >= '2017-01-12' and endDate <= '2017-04-27' 
and datename(WEEKDAY,startdate))<>'Friday' 
+0

For mor e可以理解,请填写给定日期的整个查询。 – TanvirArjel

+0

@TanvirArjel:看到更新,但不知道为什么你有两个日期,它是如何影响结果 – TheGameiswar

+0

我需要使用DATEDIFF()函数作为值不是来自任何表.. – TanvirArjel

相关问题