I am making a program that auto-runs using windows scheduler. What I'd like to do is set the program to run on the 1st and the 16th of every month. If the program run's on the 1st. I'd like to have the query run for last month... For example if today was the first of august I would want it to run 7/1/12 - 7/31/12. If I run the program on the 16th I want it to run the query for the current month to the 15th. For example if it were 8/16, I would want the program to run the query for 8/1/12 - 8/15/12.
Below is my query. It works great except it does not get the 1st of the month. (if it's the 15th or the 1st)
For example: if I run the query on 7/1/12 it should send the results for 6/1-6/30 Right now it's showing 6/2-6/30. If I run the query for 6/15 it should run 6/1 -6/15... it's running it for 6/2 - 6/15.
SELECT
Store_Number, Invoice_Number, Invoice_Date, Extended_Price,
Warranty_Amount, Quantity_Sold, Invoice_Detail_Code
FROM
Invoice_Detail_Tb
WHERE
(Invoice_Date BETWEEN (CASE WHEN datepart(dd, getdate()) = 1 THEN dateadd(mm, - 1, getdate()) ELSE dateadd(dd, - 15, getdate()) END)
AND (CASE WHEN datepart(dd, getdate()) = 1 THEN dateadd(dd, - 1, getdate()) ELSE dateadd(dd, - 1, getdate()) END))
AND (Warranty_Amount > 0)
AND (Store_Number = '309')
ORDER BY
Store_Number, Invoice_Date
Any idea how to get the first of the month included in there?