SQL date functions allow you to manipulate date and time effectively. You can use the date functions for various operations such as adding days to a date, calculating the difference between two dates, or extracting a part of a date.
Section 1. Getting the Current Date & Time Functions #
This section shows you how to get the current date, time, and timestamp value using various date and time retrieval 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.
Section 2. Extracting Date & Time Components #
This section shows you how to use the EXTRACT
and DATEPART
functions to extract a specific component such as year, month, day, hour, minute, and second from a date, time, or timestamp value.
- EXTRACT – Extracts a specific part of a date and time value including year, month, day, hour, minute, and second.
- Extract a year from a date.
- Extract a month from a date.
- Extract a day from a date.
- DATEPART – Extract a part of a date such as a year, month, and day from a given date.
Section 3. Arithmetic Functions #
This section introduces the functions that perform date arithmetic operations, including adding and subtracting.
- DATEADD – Adds an interval to a date/time value.
- DATEDIFF – Returns the difference between two dates or times.
- DATE_TRUNC – Truncates a date/time value to a specified precision such as year, month, day, hour, minute, and second.
Section 4. Date and Time Conversion Functions #
This section shows you how to convert between a date and time and non-datetime values.