sql 以工作日計算起始年假的計算時間範圍

 1 --以工作日計算起始年假的計算時間範圍
 2 declare @settoday smalldatetime,@birthday smalldatetime,@year varchar(10),@month varchar(10),@day varchar(10),@enddate datetime,@stardate datetime,@AnnualNumber int,@AnnualTotal int,@Surplus int
 3  set @settoday='2013-05-04'--現在日期
 4 set @birthday='2004-04-26' ---進公司日期
 5 set @year=DateName(Year,@settoday)
 6 set @month=DateName(Month,@birthday)
 7 set @day=DateName(Day,@birthday)
 8 --
 9 set @enddate=cast(@year+'-'+@month+'-'+@day as datetime)
10 select @month
11 select @enddate
12 if(@enddate>=@settoday)
13 begin
14     set @stardate=dateadd(year,-1,@enddate) --減一年
15 end 
16 else
17 begin
18     set @enddate=dateadd(year,1,@enddate)
19     set @stardate=cast(@year+'-'+@month+'-'+@day as datetime)
20 end
21 select @stardate AS '起始計算日期'
22 select @enddate AS '結束計算日期'

 

你可能感兴趣的:(sql)