SQL Date functions will vary from platform to platform. It is important to know what format of the date and/or time is stored in the database so that you can use the correct function to query the information. There are a variety of built-in functions we can use to query SQL.
Most Common Microsoft SQL Server Date Functions
Function | Description |
---|---|
GETDATE() | Returns the current date and time |
DATEPART() | Returns a single part of a date/time |
DATEADD() | Adds or subtracts a time interval from a date |
DATEDIFF() | Returns the time between two dates |
CONVERT() | Displays date/time data in different formats |
Most Common MySQL Date Functions
Function | Description |
---|---|
NOW() | Returns the current date and time |
CURDATE() | Returns the current date |
CURTIME() | Returns the current time |
DATE() | Returns the date part of a date or date/time |
EXTRACT() | Returns a single part of a date/time |
DATE_ADD() | Adds a specified time interval to a date |
DATE_SUB() | Subtracts a specified time interval from a date |
DATEDIFF() | Returns the number of days between two dates |
DATE_FORMAT() | Displays date/time data in different formats |
Understanding which functions to use, and how to appropriately use it on which table field is somewhat challenging, but once you are very familiar with these functions, you will find them to be quite powerful.