Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHERE Clause to find all records in a specific month

I want to be able to give a stored procedure a Month and Year and have it return everything that happens in that month, how do I do this as I can't compare between as some months have different numbers of days etc?

What's the best way to do this? Can I just ask to compare based on the year and month?

Thanks.

like image 566
Tarks Avatar asked May 12 '09 05:05

Tarks


People also ask

How do I find the record for a specific date in SQL?

You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.

Does WHERE clause used to filter records?

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.


1 Answers

I think the function you're looking for is MONTH(date). You'll probably want to use 'YEAR' too.

Let's assume you have a table named things that looks something like this:

id happend_at -- ---------------- 1  2009-01-01 12:08 2  2009-02-01 12:00 3  2009-01-12 09:40 4  2009-01-29 17:55 

And let's say you want to execute to find all the records that have a happened_at during the month 2009/01 (January 2009). The SQL query would be:

SELECT id FROM things     WHERE MONTH(happened_at) = 1 AND YEAR(happened_at) = 2009 

Which would return:

id --- 1 3 4 
like image 104
Shalom Craimer Avatar answered Oct 13 '22 23:10

Shalom Craimer