bitbucket-25253 (8/5/2012)
Is this what you are looking for:
DECLARE @D DATETIME
SET @D = '2011-12-24' -- user passed value
SELECT SUM(Salesweek) FROM Sales WHERE Weekenddate BETWEEN DATEADD(ww,-3,@D) AND @D
Result: 9500
Now if the user passes in an improper date, for example
SET @D = '2011-12-23'
SELECT SUM(Salesweek) FROM Sales WHERE Weekenddate BETWEEN DATEADD(ww,-3,@D) AND @D
Result: 8500
Your T-SQL that executes the code, should check that the user passed value is a valid weekenddate. You could use an IF EXISTS statement to check that and return a message if the date is not valid.
Is there any way to get the rid for invalid weekend date.
Let's say that if user will give the date '2011-12-23' and it should take the closer weekend date for the given date.