date_format function

The date_format function allows you to easily switch between various granularities:

Select everything from the same day:

select * from table 
where date_format(date, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d');

From the same month:

select * from table 
where date_format(date, '%Y-%m') = date_format(now(), '%Y-%m');

From the same year:

select * from table 
where date_format(date, '%Y') = date_format(now(), '%Y');

From the same hour:

select * from table 
where date_format(date, '%Y-%m-%d %H') = date_format(now(), '%Y-%m-%d %H');

and so on.

Posted In SQL

One thought on “date_format function

Leave a Reply

www.000webhost.com