SQL Server 2012:DateDiff 在 where 子句中不包括周末
SQL Server 2012: DateDiff excluding weekends in where clause
我有一个动态 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)'
但我想更改它,以便它只计算工作日期,并检查 office
是在周末是星期六和星期日的国家/地区还是在其他国家/地区
周末是周五和周六。我可以从 Offices
table 获取办公室所在的国家/地区。办公室可以在黎巴嫩(周六至周日周末)或沙特阿拉伯(周五至周六周末)
也许你可以尝试这样的事情:
首先从 Offices
表中找到 @Officeid
的 Country
:
-- 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
学分
Jeff Moden
我有一个动态 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)'
但我想更改它,以便它只计算工作日期,并检查 office
是在周末是星期六和星期日的国家/地区还是在其他国家/地区
周末是周五和周六。我可以从 Offices
table 获取办公室所在的国家/地区。办公室可以在黎巴嫩(周六至周日周末)或沙特阿拉伯(周五至周六周末)
也许你可以尝试这样的事情:
首先从 Offices
表中找到 @Officeid
的 Country
:
-- 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
学分 Jeff Moden