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.
Function | Description |
---|---|
CURDATE | Returns the current date. |
DATEDIFF | Calculates the number of days between two DATE values. |
DAY | Gets the day of the month of a specified date. |
DATE_ADD | Adds a time value to date value. |
DATE_SUB | Subtracts a time value from a date value. |
DATE_FORMAT | Formats a date value based on a specified date format. |
DAYNAME | Gets the name of a weekday for a specified date. |
DAYOFWEEK | Returns the weekday index for a date. |
EXTRACT | Extracts a part of a date. |
LAST_DAY | Returns the last day of the month of a specified date |
NOW | Returns the current date and time at which the statement executed. |
MONTH | Returns an integer that represents a month of a specified date. |
STR_TO_DATE | Converts a string into a date and time value based on a specified format. |
SYSDATE | Returns the current date. |
TIMEDIFF | Calculates the difference between two TIME or DATETIME values. |
TIMESTAMPDIFF | Calculates the difference between two DATE or DATETIME values. |
WEEK | Returns a week number of a date. |
WEEKDAY | Returns a weekday index for a date. |
YEAR | Return the year for a specified date |