SQL Server 2012:DateDiff不包括where子句中的周末

SQL Server 2012:DateDiff不包括where子句中的周末,sql,sql-server,sql-server-2012,dynamic-sql,datediff,Sql,Sql Server,Sql Server 2012,Dynamic Sql,Datediff,我有一个动态SQL查询,它统计特定办公室中签名超过创建日期1个工作日的文档 以下是查询: set @strsql = '(select @cnt = COUNT(*) from '+@TableNameDocs+' D inner join dbo.Signatures S on D.id = S.TableId where S.cityid = '+str(@Cityid)+' and S.OfficeId = '+str(@Of

我有一个动态SQL查询,它统计特定办公室中签名超过创建日期1个工作日的文档

以下是查询:

set @strsql = '(select @cnt = COUNT(*) from '+@TableNameDocs+' D
                inner join dbo.Signatures S on D.id = S.TableId
                where S.cityid = '+str(@Cityid)+' and S.OfficeId = '+str(@Officeid)+' and S.isValid = 1 and D.cityid = '+str(@Cityid)+' and D.OfficeId = '+str(@Officeid)+' and DATEDIFF(day,D.CreatedDate,COALESCE(S.SignedDate, GETDATE())) > 1)'
但我想更改它,这样它只计算工作日期,并检查
办公室是否在周末为周六和周日的国家或某个国家

周末是星期五和星期六。我可以从
Offices
表中获取办公室所在国家/地区。办公室可以在黎巴嫩(周六-周日周末)或沙特阿拉伯(周五-周六周末)

也许你可以试试这样的方式: 首先从
办公室
表中找到
@Officeid
国家

-- GET OFFICE'S COUNTRY
    SELECT @country = [Country] from dbo.Offices
    where officeid = @Officeid
然后根据国家:

IF @country = 'SAUDI ARABIA' --Weekend in Saudi Arabia is Fri-Sat
    BEGIN
        set @strsql = '(select @cnt=COUNT(*) from '+@TableNameDocs+' D
                        inner join dbo.Signatures S on D.id = S.TableId
                        where S.cityid = '+str(@Cityid)+' and S.OfficeId = '+str(@Officeid)+' and S.isValid = 1 and D.cityid = '+str(@Cityid)+' and D.OfficeId = '+str(@Officeid)+' and 
                        ((DATEDIFF(dd, D.CreatedDate, COALESCE(S.SignedDate, GETDATE())))
                        -(DATEDIFF(wk, D.CreatedDate, COALESCE(S.SignedDate, GETDATE())) * 2)
                        -(CASE WHEN DATENAME(dw, D.CreatedDate) = ''Saturday'' THEN 1 ELSE 0 END)
                        -(CASE WHEN DATENAME(dw, COALESCE(S.SignedDate, GETDATE())) = ''Friday'' THEN 1 ELSE 0 END))>1)'

    END
    else
    BEGIN
        set @strsql = '(select @cnt=COUNT(*) from '+@TableNameDocs+' D
                        inner join dbo.Signatures S on D.id = S.TableId
                        where S.cityid = '+str(@Cityid)+' and S.OfficeId = '+str(@Officeid)+' and S.isValid = 1 and D.cityid = '+str(@Cityid)+' and D.OfficeId = '+str(@Officeid)+' and 
                            ((DATEDIFF(dd, D.CreatedDate, COALESCE(S.SignedDate, GETDATE())))
                            -(DATEDIFF(wk, D.CreatedDate, COALESCE(S.SignedDate, GETDATE())) * 2)
                            -(CASE WHEN DATENAME(dw, D.CreatedDate) = ''Sunday'' THEN 1 ELSE 0 END)
                            -(CASE WHEN DATENAME(dw, COALESCE(S.SignedDate, GETDATE())) = ''Saturday'' THEN 1 ELSE 0 END))>1)'
    END
学分

这可能会有帮助@Sureshraprajapati谢谢,但我在where子句中使用所需的if语句时遇到问题