我有以下两个日期:获取的时间差不包括在SQL Server周五两个日期之间
startDate = '2017-04-04' and endDate = '2017-04-12'
我想找到使用SQL Server这两个日期之间的天排除'Fridays'
的总数。
任何来自SQL Server专家的帮助将不胜感激!提前致谢!!
我有以下两个日期:获取的时间差不包括在SQL Server周五两个日期之间
startDate = '2017-04-04' and endDate = '2017-04-12'
我想找到使用SQL Server这两个日期之间的天排除'Fridays'
的总数。
任何来自SQL Server专家的帮助将不胜感激!提前致谢!!
您可以使用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
略低于条款添加到您的查询
select count(*) from table
where startDate >= '2017-01-12' and endDate <= '2017-04-27'
and datename(WEEKDAY,startdate))<>'Friday'
For mor e可以理解,请填写给定日期的整个查询。 – TanvirArjel
@TanvirArjel:看到更新,但不知道为什么你有两个日期,它是如何影响结果 – TheGameiswar
我需要使用DATEDIFF()函数作为值不是来自任何表.. – TanvirArjel
它的未来8.什么应该根据你的答案? –
它应该是7,因为他们之间有一个星期五..用它来检查.... SELECT DATEDIFF(day,'2017-04-04','2017-04-12')AS DiffDate..This is returned 8 ..你的代码应该返回7 .. – TanvirArjel
datediff检查你需要在任一方向跨越达到日期的边界。 –