Home > mysql > Queries using datetime in mysql to search by month

Queries using datetime in mysql to search by month

I recently started working with a sales database and rather than using my personal favorite timestamp column type they used datetime – it took me a couple googles before I finally found the syntax to do queries to find date ranges:

SELECT * FROM `sales` WHERE MONTH(column_name) = 6 AND DAY(column_name) < 10;

Will get all the sales before the 10th in the month of June and ANY year šŸ˜€ not so great if you have more than a years data hehe šŸ˜› but now you know how it works and to find year is YEAR(column_name)

If you wanted to get all records that were for this year you can use:

Select * from table_name where field_name > YEAR(CURDATE());

Categories: mysql
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment