SQL Date Functions

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 3. Arithmetic Functions #

  • DATEADD – Adds an interval to a date/time value.
  • DATEDIFF – Returns the difference between two dates or times.
  • TIMESTAMPDIFF – Returns the differences between two timestamps.

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.


NameDescription
CURRENT_DATEReturn the current date
CURRENT_TIMEReturn the current time
CURRENT_TIMESTAMPReturn the current date and time
Convert date to stringUse the CAST() or TO_CHAR() function to convert a date to a string
Convert string to dateUse the CAST() or TO_DATE() function to convert a string to a date based on a specified format
DATEADDAdd an interval to a date
DATEDIFFFind the difference between the two dates
DATEPARTExtract a part of a date such as a year, month, and day from a given date
Extract Year from DateExtract the year from a date
Extract Month from DateExtract the month from a date
Extract Day from DateExtract the day of the month from a date