In MySQL Date Or Time Function Part-3 we will discuss about the DATE_SUB Function, DATEDIFF function, DAY Function, DAYNAME Function, DAYOFMONTH Function
DATE_SUB Function:- DATE_SUB Function is used for returning the date after subtracting the date or time interval.
Syntax:- DATE_SUB(date, INTERVAL value addunit)
We can use following add Unit:-
MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, SECOND_MICROSECOND, MINUTE_MICROSECOND, MINUTE_SECOND, HOUR_MICROSECOND, HOUR_SECOND, HOUR_MINUTE, DAY_MICROSECOND, DAY_SECOND, DAY_MINUTE, DAY_HOUR, YEAR_MONTH
Example:-
SELECT DATE_SUB(“2023-02-07”, INTERVAL 7 DAY);
Output:- 2023-01-31
SELECT DATE_SUB(“2023-02-07 08:07:11”, INTERVAL 7 SECOND);
Output:- 2023-02-07 08:07:04
DATEDIFF Function:- The DATEDIFF function is used for returning a number of days between a given two date values.
Syntax:- DATEDIFF(date1, date2)
Example:-
SELECT DATEDIFF(“2023-02-06”, “2023-01-20”);
Output:- 17
SELECT DATEDIFF(“2023-02-06 07:32:18”, “2023-01-20 12:20:30”);
Output:- 17
DAY Function:- DAY Function is used for returning the day of the month from a given date.
Syntax:- DAY(date)
Example:-
SELECT DAY(“2023-02-07”);
Output:- 7
SELECT DAY(“2023-02-07 07:32:18”);
Output:- 7
DAYNAME Function:- DAYNAME Function is used for returning weekday names from a given date.
Syntax:- DAYNAME(date)
Example:-
SELECT DAYNAME(“2023-02-08”);
Output:- Wednesday
DAYOFMONTH Function:- DAYOFMONTH Function is used for returning the day of the month(1-31) from a given date. This function is similar to the DAY function.
Syntax:- DAYOFMONTH(date)
Example:-
SELECT DAYOFMONTH(“2023-02-08”);
Output:- 8
Read Also:-
MySQL Date Or Time Function Part-1
MySQL Date Or Time Function Part-2
MySQL Date Or Time Function Part-4
MySQL Date Or Time Function Part-5
MySQL Date Or Time Function Part-6
MySQL Date Or Time Function Part-7
MySQL Date Or Time Function Part-8
Read more detail about MySQL Date Or Time Function manual