---2013年10月9日生日,就以2012年9月1日至2013年8月31日計算 (因為係生日月份前兩個月之最後一天為結算日) DECLARE @birthday datetime,@now datetime,@stat datetime,@end datetime,@statbirthday datetime,@endbirthday datetime,@thirdbirthday datetime,@firthbirthday datetime, @year int,@month int , @day int,@str varchar(20),@total decimal set @now=getdate() set @year=Year(getdate()) set @birthday='1992-10-14' set @month=month(@birthday) set @day=day(@birthday) set @str=cast(@year as varchar(4))+'-'+cast(@month as varchar(2))+'-'+cast(@day as varchar(2)) set @birthday=cast(@str as datetime) --會員生日有效生首日起至3個月內有效 set @firthbirthday=DATEADD(mm, DATEDIFF(mm,0,@birthday), 0) --当月的第一天 set @thirdbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+3, 0)) --上三个月最后的一天 set @statbirthday=DATEADD(mm, DATEDIFF(mm,0,@birthday)-13, 0) set @endbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,@birthday)-1, 0)) select @statbirthday,@endbirthday,@firthbirthday,@thirdbirthday SELECT @total=ISNULL(SUM(amount),0) FROM View_birthdayVipdlyList WHERE indate>@statbirthday AND indate<@endbirthday and vipno='654321' select @total*.01