Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

My query:

SELECT *
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)
0
Comment
Question by:matthewdacruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

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

SELECT *
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.

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

Author Comment

by:matthewdacruz
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?
0
 
LVL 93

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:

SELECT *
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

0
 
LVL 24

Accepted Solution

by:
mankowitz earned 2000 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:

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

Author Closing Comment

by:matthewdacruz
ID: 36568297
Thanks that worked perfect.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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