Diasline Diasline - 3 months ago 8
MySQL Question

Select month from datetime column

I have a table that has a datetime column named :completed_date (2016-05-27 04:08:53). I would like to select all data for this current month while ignoring the time. Something like this:

$query = mysqli_query($dbc, " select * from eob_posting where
completed_date=DATE(NOW())");


How can i remove the time so that the clause where considere only the date ?

Answer

Even shorter than the other answers :-)

Note that a datetime is basically also a string of the form "2016-01-01 12:34:56", so you can just:

> SELECT LEFT(NOW(), 7) as YearAndMonth;
YearAndMonth
------------
2016-08

So your query is easily expressed as:

SELECT * FROM eob_posting
WHERE LEFT(completed_date, 7) = LEFT(NOW(), 7)

Happy hunting!

Comments