Query - 1st to lastday of the month

VBdotnet2005
VBdotnet2005 used Ask the Experts™
on
I have a Windows app, this query DB and put a data in Excel. I schedule to run on last day of month in Win Scheduler task... the 30th.
If it run on the 30th, will it query from the 1st to 30th.? Actually, I really want it to query from the 1st to the lastday of the month, but not sure how.

Select mycustomer, dateordered, etc...from table where month(ordersubmitted) = datepart(month, getdate()) and year(ordersubmitted) = datepart(year, getdate())
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Select
 mycustomer,
 dateordered,
 etc...
from table
where  ordersubmitted >= dateadd(m, datediff(m, 0, getdate()), 0) and
 ordersubmitted < dateadd(m, datediff(m, 0, getdate())+1, 0)
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018
Commented:
>>  schedule to run on last day of month in Win Scheduler task...

Since you have SQL Agent residing in SQL Server, you can use that to schedule these jobs instead of going with Windows Task Scheduler.
>>If it run on the 30th, will it query from the 1st to 30th.?<<
No, the query as you have it right now will consider the whole month. But it is very inefficient. That's why I've suggested comment http:#a33126542

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial