HomeMYSQLDATETIME🔥 Basic Date-Time Questions in MySQL

🔥 Basic Date-Time Questions in MySQL

1.Fetch today’s records

SELECT * FROM table WHERE DATE(col)=CURDATE();

2.Get records from yesterday

SELECT * FROM table WHERE col >= CURDATE()-INTERVAL 1 DAY AND col < CURDATE();

3.This month’s records

SELECT * FROM table WHERE MONTH(col)=MONTH(CURDATE()) AND YEAR(col)=YEAR(CURDATE());

4.Last 7 days

WHERE col >= CURDATE() - INTERVAL 7 DAY

5.Extract month name

SELECT MONTHNAME(col) FROM table;

6.Specific weekday (e.g. Monday)

WHERE DAYOFWEEK(col)=2

7.Time difference between TIME columns

SELECT TIMEDIFF(time_end, time_start)FROM table;

8.Birthdays today

WHERE MONTH(birthdate)=MONTH(CURDATE()) AND DAY(birthdate)=DAY(CURDATE())

9.Date part only

SELECT DATE(col) FROM table;

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *