views:

35

answers:

3

[MySQL/PHP] My table has a date column of datetime format. All records are of the YYYY-MM-DD HH:MM:SS variety.

MySQL queries like SELECT record FROM table WHERE date > '1941' AND date < '1945' work nicely.

MySQL queries like SELECT record FROM table WHERE date > '1941-03-01' AND date < '1945-01-30' also work nicely.

But what about if I wanted all records that were filed in March, regardless of year? Or all records filed on the 17th, regardless of month/year?

`SELECT record FROM table WHERE date = '03' clearly doesn't work.

I know I could snag it with a LIKE '%-03-%' parameter, but that doesn't leave room for me to search for range, like all records from March to May.

Help? :-)

+1  A: 

Try WHERE MONTH(DATE(`date`)) BETWEEN '03' AND '05'

The DATE() part is to extract the date from the timestamp to be used with MONTH().

henasraf
+1 for the answer, but giving the check to Jens since he was the first to provide a link back to the docs. Thank you!
Andrew Heath
Aw ): dang I wanted to be the answer xD
henasraf
+1  A: 

If you look at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html, you will find many useful functions for your purpose. ... WHERE MONTH(date) = 3, e.g. =)

Jens
Thank you Jens!
Andrew Heath
+2  A: 

You can use MySQL date functions:

SELECT record FROM table WHERE MONTH(date) = 3
SELECT record FROM table WHERE DAY(date) = 17

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Silver Light
+1 for you too, but giving the check to Jens because he was a few seconds faster. Thank you!
Andrew Heath