Mysql How do you define a date range for the current month

Posted on 2011-09-20
Last Modified: 2012-05-12
I am wanting to write a mysql query that only looks at the current month.
How would you write this where clause

My query:

FROM Task_assigned
Where Completed = 0 and DateDue between (this is where I do not know how to assign the range for the current month)
Question by:matthewdacruz
  • 2
  • 2
LVL 24

Expert Comment

ID: 36567660
if all you want is the month, then use the MYSQL month function. Seel below for january

FROM Task_assigned
Where Completed = 0 and MONTH(DateDue ) = 1

if all you want a specific month, you have to include the year as well.

FROM Task_assigned
Where Completed = 0 and MONTH(DateDue ) = 1 and YEAR(DateDue)=2011

Author Comment

ID: 36567725
Hi Thank you for your response.

Is there a way to dynamically define the month and year as the report needs to automatically grab each months tasks without having to  hard code it in?
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36567767
In SQL Server you could do this with DATEDIFF/DATEADD; I presume something similar can be done in MySQL:

FROM Task_assigned
Where Completed = 0 and 
    DateDue >= DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()), '2000-01-01') AND
    DateDue < DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) + 1, '2000-01-01')

Open in new window

LVL 24

Accepted Solution

mankowitz earned 500 total points
ID: 36567872
I am not sure what you mean by dynamically. Are you calling this query from a scripting language? if so, your script could do it. Alternatively, if you want it to just print the current month, you could do this:

FROM Task_assigned
Where Completed = 0 and MONTH(DateDue ) = month(NOW()) and YEAR(DateDue)=YEAR(NOW())

Author Closing Comment

ID: 36568297
Thanks that worked perfect.

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 34 99
Mysql sync between 3-4 mysql db 4 32
Foxpro errors 4 30
Simple SQL query from two tables 13 54
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question