SQL date functions allow you to manipulate date and time data effectively. You can use the date functions for various operations such as adding days to a date, calculating the difference between two dates, or getting parts of a date.
Section 1. Getting Date & Time Functions #
- CURRENT_DATE – Returns a date value that represents the current date.
- CURRENT_TIME – Returns the time value that represents the current time.
- CURRENT_TIMESTAMP – Returns a timestamp that represents the current date and time.
- LOCALTIME – Returns a time value that represents the local time.
- LOCALTIMESTAMP – Returns the timestamp representing the current local date and time.
Section 2. Extracting Date & Time Components #
- EXTRACT – Extracts a specific part of a date and time value including year, month, day, hour, minute, and second.
Section 4. Formatting dates and times #
- FORMAT_DATE – Formats a date based on a format.
- FORMAT_TIME – Formats a time based on a format.
- FORMAT_TIMESTAMP – Formats a timestamp value based on a specific pattern.
Section 5. Date and Time Conversion Functions #
- CAST- Converts a date or time expression into a specific type.
Section 6. Truncating Functions #
- DATE_TRUNC – Truncates a date/time value to a specified precision such as year, month, day, hour, minute, and second.
Section 7. Age & Duration Functions #
- AGE – Returns the difference (age) between two timestamps.
- DURATION – Returns the duration between two dates and times.
Section 8. Checking Functions #
- IS_DATE – Returns true if a string is a valid date value.
- IS_TIME – Returns true if a string is a valid time.
- IS_TIMESTAMP – Returns true if a string is a valid timestamp.
Name | Description |
CURRENT_DATE | Return the current date |
CURRENT_TIME | Return the current time |
CURRENT_TIMESTAMP | Return the current date and time |
Convert date to string | Use the CAST() or TO_CHAR() function to convert a date to a string |
Convert string to date | Use the CAST() or TO_DATE() function to convert a string to a date based on a specified format |
DATEADD | Add an interval to a date |
DATEDIFF | Find the difference between the two dates |
DATEPART | Extract a part of a date such as a year, month, and day from a given date |
Extract Year from Date | Extract the year from a date |
Extract Month from Date | Extract the month from a date |
Extract Day from Date | Extract the day of the month from a date |