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.

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

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]