按时间段获取星期几

create proc proc_range
(
 @starttime varchar(20),
@endtime varchar(20)

)
as
begin

declare   @b   datetime,@e   datetime   
  set   @b   =  @starttime    
  set   @e   =      @endtime
    
  select   datediff(week,@b,@e)  as 几个星期 
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   1   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   1   then   0   else   1   end   as   周日数   
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   2   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   2   then   0   else   1   end   as   周一数   
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   3   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   3   then   0   else   1   end   as   周二数   
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   4   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   4   then   0   else   1   end   as   周三数   
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   5   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   5   then   0   else   1   end   as   周四数   
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   6   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   6   then   0   else   1   end   as   周五数   
  ,datediff(week,@b,@e)   +   case   when   datepart(weekday,@b)   >   7   then   0   else   1   end   -   case   when   datepart(weekday,@e)   >=   7   then   0   else   1   end   as   周六数 

end 

exec proc_range '2009-12-01','2009-12-18'
posted @ 2009-12-11 17:37  余魁  阅读(274)  评论(0编辑  收藏  举报