如何计算一个学期中不包括周末的总天数 [英] how to calculate total number of days in a term excluding weekends

查看:136
本文介绍了如何计算一个学期中不包括周末的总天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Declare 
 @counter int = 0,
 @strt date,
 @end date
Begin 
set @strt=(Select Distinct StartDate
		   From SchoolTerms
		   where EmisCode =''500226884'' 
		   AND CurrentYear=''2009''
		   AND Quater=''Term1'')
Set @end=(Select Distinct EndDate
		   From SchoolTerms
		   where EmisCode =''500226884'' 
		   AND CurrentYear=''2009''
		   AND Quater=''Term1'')
while (@strt<@end)
if DATEPART(weekday,@strt)<>7 or DATEPART(weekday,@strt)<> 1 
	set @counter+=1
	set @strt=@strt-1
print @strt
print @end
print @counter
End;



我有以下代码可计算一个学期的总天数,但出现以下错误.



i''ve got the following code for calculating total number of days in a term but i get the following error.

Msg 206, Level 16, State 2, Line 19
Operand type clash: date is incompatible with int

推荐答案

set @strt=@strt-1

导致了此问题,因为您尝试从日期中减去1.使用 DATEADD [ DATEDIF [

is causing the problem because you are try to subtract 1 from a date. Use DATEADD[^] if you want to subtract one day.

Have a look at DATEDIF[^] for a better way to calculate the number of days.


最好的方法是创建日历表(您也可以跟踪假期).
The best way to do it would be creating a calendar table (you will be able to track holidays too).


这篇关于如何计算一个学期中不包括周末的总天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆