获取SQL Server

时间:2017-04-04 09:13:44

标签: sql sql-server

我有以下两个日期:

startDate = '2017-04-04'  and  endDate = '2017-04-12'

我想找到使用SQL Server排除'Fridays'这两个日期之间的总天数。

来自SQL Server专家的任何帮助都将受到高度赞赏!提前致谢!!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

只需在查询中添加以下子句

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