Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select records from current month

Tags:

date

php

mysql

I have a table with a varchar field called slldate. This field contains dates in this format:
2010-08-30
(YYYY-MM-DD)

Now I would like to select the records containing the current month using a mysql query.

Anyone?

like image 963
Dreni Avatar asked Nov 18 '25 14:11

Dreni


2 Answers

as you used a char field instead a date field, you have to cast the value and then use the normal date functions. like

SELECT * FROM table WHERE MONTH(CAST(slidate as date)) = MONTH(NOW()) AND YEAR(CAST(slidate as date)) = YEAR(NOW())
like image 91
Rufinus Avatar answered Nov 20 '25 05:11

Rufinus


Try this:

SELECT * FROM table_name WHERE MONTH(slldate) = date('m') AND YEAR(slldate) = date('Y');

like image 41
Murugesh Avatar answered Nov 20 '25 03:11

Murugesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!