Jenna Maiz Jenna Maiz - 3 months ago 11
MySQL Question

SQL: How to get entries with the same month and year as the table's timestamp field?

I have the following table

+--------------------------------------+---------------------+---------------------+----------+-----------------------------+----------+--------+
| id | app_start | app_end | title | body | location | allDay |
+--------------------------------------+---------------------+---------------------+----------+-----------------------------+----------+--------+
| 52e782d7-8da8-4b27-a17e-d6beb72649e8 | 2004-04-27 10:30:00 | 2005-02-18 11:00:00 | 10 years | Something. | NY,USA | 0 |
| a3e11a10-26d3-407d-83b3-c3cd6cda128f | 2004-02-17 10:30:00 | 2004-02-17 11:00:00 | 12 years | Insert random message here | NY,USA | 0 |
+--------------------------------------+---------------------+---------------------+----------+-----------------------------+----------+--------+


Here the column
app_start
is of type
timestamp
. If I want to get rows for a specific month and year, say month 2(Feb) of 2004 which would return the second row, what would I insert in the
where
clause?

SELECT * FROM table WHERE [????]


Thanks for any help.

Answer

Just use month and year:

select *
from yourtable
where month(app_start) = 2 and year(app_start) = 2004