Home Forums SQL Server 2008 T-SQL (SS2K8) How to count the total sales prior's 4 weekend if user give the weekend date. RE: How to count the total sales prior's 4 weekend if user give the weekend date.

  • 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.