Skip to main content

SQL Date Functions

Assuming you've got some dates properly stored as a date or time data type, you can do some pretty powerful things. Maybe you'd like to calculate a field of dates a week after an existing field. Or maybe you'd like to create a field that indicates how many days apart the values in two other date fields are. These are trivially simple, but it's important to keep in mind that the data type of your results will depend on exactly what you are doing to the dates.

When you perform arithmetic on dates (such as subtracting one date from another), the results are often stored as the interval data type—a series of integers that represent a period of time.

FunctionDescription
CURDATEReturns the current date.
DATEDIFFCalculates the number of days between two DATE values.
DAYGets the day of the month of a specified date.
DATE_ADDAdds a time value to date value.
DATE_SUBSubtracts a time value from a date value.
DATE_FORMATFormats a date value based on a specified date format.
DAYNAMEGets the name of a weekday for a specified date.
DAYOFWEEKReturns the weekday index for a date.
EXTRACTExtracts a part of a date.
LAST_DAYReturns the last day of the month of a specified date
NOWReturns the current date and time at which the statement executed.
MONTHReturns an integer that represents a month of a specified date.
STR_TO_DATEConverts a string into a date and time value based on a specified format.
SYSDATEReturns the current date.
TIMEDIFFCalculates the difference between two TIME or DATETIME values.
TIMESTAMPDIFFCalculates the difference between two DATE or DATETIME values.
WEEKReturns a week number of a date.
WEEKDAYReturns a weekday index for a date.
YEARReturn the year for a specified date