I have a C# program that will run as a windows scheduled task. This program will load, run a SQL Query, email the results stored in the dataset, and then close. I have everything except the using Yesterdays date.
Here is my current Query:
SELECT Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number, @startdate AS Start, @enddate AS Today
FROM Paid_Out_Tb
WHERE (Store_Id = 1929) AND (Paid_Out_Datetime BETWEEN @startdate AND @enddate)
Obviously I need to assign @startdate and @enddate at the time of the query. Since I will need 12AM to 1159PM that is the reason for the start and end. So for example. If I want to run the program today it would search yesterday (the 23rd) so @startdate would be assigned 7/22/12 00:00:00 and @enddate would be assigned 7/22/12 23:59:59...
Would it make more sense to do it in the query instead of the program? If so how would I change the query?
Don't use between
when dealing with date intervals. It is much easier and safer to use >=
and <
.
Something like this will give you yesterday stuff without parameters.
SELECT Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number, @startdate AS Start, @enddate AS Today
FROM Paid_Out_Tb
WHERE Store_Id = 1929 AND
Paid_Out_Datetime >= dateadd(day, datediff(day, 0, getdate())-1, 0) and
Paid_Out_Datetime < dateadd(day, datediff(day, 0, getdate()), 0)