Reputation: 3253
I have saved the date in the database (mysql) as (d-m-Y H:i:s)
How can write a sql query to select all the values that are only for the current month or a specific month ?
Upvotes: 1
Views: 24558
Reputation: 201
SELECT *
FROM table
WHERE DATE_FORMAT(date_field,'%m-%Y') = '07-2012';
Upvotes: 6
Reputation: 1963
SELECT * FROM `your_table` WHERE MONTH(`your_date_column`)=6
AND YEAR(`your_date_column`)=2012
Gets everything from June 2012.
Upvotes: 2
Reputation: 7597
Store your dates in MySQL DATE format and use the following query:
SELECT
*
FROM
table
WHERE
MONTH(date_field) = MONTH(CURRENT_DATE)
AND
YEAR(date_field) = YEAR(CURRENT_DATE)
Storing dates as a string/varchar is a very bad idea.
Upvotes: 3