SQL | Date Functions (Set-1)

20 Jul, 2018
In SQL, dates are complicated for newbies, since while working with a database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, datetime (time is also involved with date) is used.
Some of the important date functions have been already discussed in the previous post. The basic idea of this post is to know the working or syntax of all the date functions:
Below are the date functions that are used in SQL:
ADDDATE(): It returns a date after a certain time/date interval has been added.Syntax: SELECT ADDTIME("2018-07-16 02:52:47", "2");Output: 2018-07-16 02:52:49ADDTIME(): It returns a time / date time after a certain time interval has been added.Syntax: SELECT ADDTIME("2017-06-15 09:34:21", "2");Output: 2017-06-15 09:34:23CURDATE(): It returns the current date.Syntax: SELECT CURDATE();Output: 2018-07-16CURRENT_DATE(): It returns the current date.Syntax: SELECT CURRENT_DATE();Output: 2018-07-16CURRENT_TIME(): It returns the current time.Syntax: SELECT CURRENT_TIME();Output: 02:53:15CURRENT_TIMESTAMP(): It returns the current date and time.Syntax: SELECT CURRENT_TIMESTAMP();Output: 2018-07-16 02:53:21CURTIME(): It returns the current time.Syntax: SELECT CURTIME();Output: 02:53:28DATE(): It extracts the date value from a date or date time expression.Syntax: SELECT DATE("2017-06-15");Output: 2017-06-15DATEDIFF(): It returns the difference in days between two date values.Syntax: SELECT DATEDIFF("2017-06-25", "2017-06-15");Output: 10DATE_ADD(): It returns a date after a certain time/date interval has been added.Syntax: SELECT DATE_ADD("2018-07-16", INTERVAL 10 DAY);Output: 2018-07-16DATE_FORMAT(): It formats a date as specified by a format mask.Syntax: SELECT DATE_FORMAT("2018-06-15", "%Y");Output: 2018DATE_SUB(): It returns a date after a certain time/date interval has been subtracted.Syntax: SELECT DATE_SUB("2017-06-15", INTERVAL 10 DAY);Output: 2018-07-16DAY(): It returns the day portion of a date value.Syntax: SELECT DAY("2018-07-16");Output: 16DAYNAME(): It returns the weekday name for a date.Syntax: SELECT DAYNAME('2008-05-15');Output: ThursdayDAYOFMONTH(): It returns the day portion of a date value.Syntax: SELECT DAYOFMONTH('2018-07-16');Output: 16DAYWEEK(): It returns the weekday index for a date value.Syntax: SELECT WEEKDAY("2018-07-16");Output: 0DAYOFYEAR(): It returns the day of the year for a date value.Syntax: SELECT DAYOFYEAR("2018-07-16");Output: 197EXTRACT(): It extracts parts from a date.Syntax: SELECT EXTRACT(MONTH FROM "2018-07-16");Output: 7FROM_DAYS(): It returns a date value from a numeric representation of the day.Syntax: SELECT FROM_DAYS(685467);Output: 1876-09-29HOUR(): It returns the hour portion of a date value.Syntax: SELECT HOUR("2018-07-16 09:34:00");Output: 9LAST_DAY(): It returns the last day of the month for a given date.Syntax: SELECT LAST_DAY('2018-07-16');Output: 2018-07-31LOCALTIME(): It returns the current date and time.Syntax: SELECT LOCALTIME();Output: 2018-07-16 02:56:42LOCALTIMESTAMP(): It returns the current date and time.Syntax: SELECT LOCALTIMESTAMP();Output: 2018-07-16 02:56:48MAKEDATE(): It returns the date for a certain year and day-of-year value.Syntax: SELECT MAKEDATE(2009, 138);Output: 2009-05-18MAKETIME(): It returns the time for a certain hour, minute, second combination.Syntax: SELECT MAKETIME(11, 35, 4);Output: 11:35:04
ADDDATE(): It returns a date after a certain time/date interval has been added.Syntax: SELECT ADDTIME("2018-07-16 02:52:47", "2");Output: 2018-07-16 02:52:49
Syntax: SELECT ADDTIME("2018-07-16 02:52:47", "2");
Output: 2018-07-16 02:52:49
ADDTIME(): It returns a time / date time after a certain time interval has been added.Syntax: SELECT ADDTIME("2017-06-15 09:34:21", "2");Output: 2017-06-15 09:34:23
Syntax: SELECT ADDTIME("2017-06-15 09:34:21", "2");
Output: 2017-06-15 09:34:23
CURDATE(): It returns the current date.Syntax: SELECT CURDATE();Output: 2018-07-16
Syntax: SELECT CURDATE();
Output: 2018-07-16
CURRENT_DATE(): It returns the current date.Syntax: SELECT CURRENT_DATE();Output: 2018-07-16
Syntax: SELECT CURRENT_DATE();
Output: 2018-07-16
CURRENT_TIME(): It returns the current time.Syntax: SELECT CURRENT_TIME();Output: 02:53:15
Syntax: SELECT CURRENT_TIME();
Output: 02:53:15
CURRENT_TIMESTAMP(): It returns the current date and time.Syntax: SELECT CURRENT_TIMESTAMP();Output: 2018-07-16 02:53:21
Syntax: SELECT CURRENT_TIMESTAMP();
Output: 2018-07-16 02:53:21
CURTIME(): It returns the current time.Syntax: SELECT CURTIME();Output: 02:53:28
Syntax: SELECT CURTIME();
Output: 02:53:28
DATE(): It extracts the date value from a date or date time expression.Syntax: SELECT DATE("2017-06-15");Output: 2017-06-15
Syntax: SELECT DATE("2017-06-15");
Output: 2017-06-15
DATEDIFF(): It returns the difference in days between two date values.Syntax: SELECT DATEDIFF("2017-06-25", "2017-06-15");Output: 10
Syntax: SELECT DATEDIFF("2017-06-25", "2017-06-15");
Output: 10
DATE_ADD(): It returns a date after a certain time/date interval has been added.Syntax: SELECT DATE_ADD("2018-07-16", INTERVAL 10 DAY);Output: 2018-07-16
Syntax: SELECT DATE_ADD("2018-07-16", INTERVAL 10 DAY);
Output: 2018-07-16
DATE_FORMAT(): It formats a date as specified by a format mask.Syntax: SELECT DATE_FORMAT("2018-06-15", "%Y");Output: 2018
Syntax: SELECT DATE_FORMAT("2018-06-15", "%Y");
Output: 2018
DATE_SUB(): It returns a date after a certain time/date interval has been subtracted.Syntax: SELECT DATE_SUB("2017-06-15", INTERVAL 10 DAY);Output: 2018-07-16
Syntax: SELECT DATE_SUB("2017-06-15", INTERVAL 10 DAY);
Output: 2018-07-16
DAY(): It returns the day portion of a date value.Syntax: SELECT DAY("2018-07-16");Output: 16
Syntax: SELECT DAY("2018-07-16");
Output: 16
DAYNAME(): It returns the weekday name for a date.Syntax: SELECT DAYNAME('2008-05-15');Output: Thursday
Syntax: SELECT DAYNAME('2008-05-15');
Output: Thursday
DAYOFMONTH(): It returns the day portion of a date value.Syntax: SELECT DAYOFMONTH('2018-07-16');Output: 16
Syntax: SELECT DAYOFMONTH('2018-07-16');
Output: 16
DAYWEEK(): It returns the weekday index for a date value.Syntax: SELECT WEEKDAY("2018-07-16");Output: 0
Syntax: SELECT WEEKDAY("2018-07-16");
Output: 0
DAYOFYEAR(): It returns the day of the year for a date value.Syntax: SELECT DAYOFYEAR("2018-07-16");Output: 197
Syntax: SELECT DAYOFYEAR("2018-07-16");
Output: 197
EXTRACT(): It extracts parts from a date.Syntax: SELECT EXTRACT(MONTH FROM "2018-07-16");Output: 7
Syntax: SELECT EXTRACT(MONTH FROM "2018-07-16");
Output: 7
FROM_DAYS(): It returns a date value from a numeric representation of the day.Syntax: SELECT FROM_DAYS(685467);Output: 1876-09-29
Syntax: SELECT FROM_DAYS(685467);
Output: 1876-09-29
HOUR(): It returns the hour portion of a date value.Syntax: SELECT HOUR("2018-07-16 09:34:00");Output: 9
Syntax: SELECT HOUR("2018-07-16 09:34:00");
Output: 9
LAST_DAY(): It returns the last day of the month for a given date.Syntax: SELECT LAST_DAY('2018-07-16');Output: 2018-07-31
Syntax: SELECT LAST_DAY('2018-07-16');
Output: 2018-07-31
LOCALTIME(): It returns the current date and time.Syntax: SELECT LOCALTIME();Output: 2018-07-16 02:56:42
Syntax: SELECT LOCALTIME();
Output: 2018-07-16 02:56:42
LOCALTIMESTAMP(): It returns the current date and time.Syntax: SELECT LOCALTIMESTAMP();Output: 2018-07-16 02:56:48
Syntax: SELECT LOCALTIMESTAMP();
Output: 2018-07-16 02:56:48
MAKEDATE(): It returns the date for a certain year and day-of-year value.Syntax: SELECT MAKEDATE(2009, 138);Output: 2009-05-18
Syntax: SELECT MAKEDATE(2009, 138);
Output: 2009-05-18
MAKETIME(): It returns the time for a certain hour, minute, second combination.Syntax: SELECT MAKETIME(11, 35, 4);Output: 11:35:04
Syntax: SELECT MAKETIME(11, 35, 4);
Output: 11:35:04
SQL-Functions
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
SQL Trigger | Student Database
How to Update Multiple Columns in Single Update Statement in SQL?
SQL Interview Questions
SQL | Views
Difference between DELETE, DROP and TRUNCATE
Window functions in SQL
MySQL | Group_CONCAT() Function
Difference between DELETE and TRUNCATE
Difference between DDL and DML in DBMS