SQL | Date Functions (Set-1)
SQL Date Functions are essential for managing and manipulating date and time values in SQL databases. They provide tools to perform operations such as calculating date differences, retrieving current dates and times and formatting dates. From tracking sales trends to calculating project deadlines, working with temporal data is central to decision-making and reporting.
In this article, we will explain these functions in detail, demonstrating their applications through practical examples using a sales table.
SQL Date Functions
SQL Date Functions are built-in utilities provided by SQL databases to perform operations on date and time values. These functions simplify handling temporal data, making them indispensable for tasks like calculating intervals, extracting year or month values, and formatting dates for display.
Let’s use the sales
table as the base for demonstrating each of the SQL Date Functions mentioned. Here’s the structure of the sales
table:

Sales Table
1. NOW()
The NOW()
function returns the current date and time based on the server’s time zone. It’s commonly used when we need to capture the exact moment an event occurs, such as a transaction timestamp. This function is useful for logging timestamps or comparing current time with database records.
Query:
SELECT NOW() AS current_datetime;
Output
current_datetime |
---|
2024-08-12 14:35:27 |
2. CURDATE()
The CURDATE()
function returns the current date in the YYYY-MM-DD
format. It is useful when we need to retrieve only the current date without the time. This function is often used in reporting or filtering records by date.
Query:
SELECT CURDATE() AS current_date;
Output
current_date |
---|
2024-08-12 |
3. CURTIME()
The CURTIME()
function returns the current time in the HH:MM:SS
format. It is useful for time-specific operations, such as scheduling tasks. By isolating the time, this function helps in scenarios requiring precise time comparisons.
Query:
SELECT CURTIME() AS current_time;
Output
current_time |
---|
14:35:27 |
4. DATE()
TheDATE()
function extracts the date portion from a date or datetime expression. This function is useful when we want to ignore the time component and focus only on the date. This function is commonly employed in date-only comparisons or aggregations.
Query:
SELECT sale_id, product_name,
DATE(sale_date) AS sale_date_only
FROM sales;
Output

Date Function
5. EXTRACT()
The EXTRACT()
function allows us to retrieve a specific part (like year, month, or day) from a date. It’s particularly useful when we need to group or filter data based on specific time components. This function is especially useful in reports that require year-over-year analysis.
Query:
SELECT sale_id, product_name,
EXTRACT(YEAR FROM sale_date)
AS sale_year FROM sales;
Output

Extract Function
6. DATE_ADD()
The DATE_ADD()
function adds a specified time interval (like days, months, or years) to a date. It’s often used in scenarios where we need to calculate a future date. This function simplifies future date calculations for planning purposes.
Query:
SELECT sale_id, product_name, DATE_ADD(sale_date, INTERVAL 7 DAY) AS sale_date_plus_7_days FROM sales;
Output
sale_id | product_name | sale_date_plus_7_days |
---|---|---|
1 | Widget A | 2024-08-08 |
2 | Widget B | 2024-08-12 |
3 | Widget C | 2024-08-14 |
4 | Widget A | 2024-08-17 |
5 | Widget B | 2024-08-22 |
6 | Widget C | 2024-08-27 |
7. DATE_SUB()
TheDATE_SUB()
function subtracts a specified time interval from a date. It’s handy when we need to determine a past date by subtracting days, months, or years. This is often used for retrospective data analysis.
Query:
SELECT sale_id, product_name,
DATE_SUB(sale_date, INTERVAL 3 DAY)
AS sale_date_minus_3_days
FROM sales;
Output
sale_id | product_name | sale_date_minus_3_days |
---|---|---|
1 | Widget A | 2024-07-29 |
2 | Widget B | 2024-08-02 |
3 | Widget C | 2024-08-04 |
4 | Widget A | 2024-08-07 |
5 | Widget B | 2024-08-12 |
6 | Widget C | 2024-08-17 |
8. DATEDIFF()
The DATEDIFF()
function returns the difference in days between two dates. It’s commonly used to calculate the duration between two events or dates. This function is ideal for deadline tracking or overdue calculations.
Query:
SELECT sale_id, product_name,
DATE_FORMAT(sale_date, '%W, %M %d, %Y') AS formatted_sale_date
FROM sales;
Output
sale_id | product_name | days_until_aug15 |
---|---|---|
1 | Widget A | 14 |
2 | Widget B | 10 |
3 | Widget C | 8 |
4 | Widget A | 5 |
5 | Widget B | 0 |
6 | Widget C | -5 |
9. DATE_FORMAT()
The DATE_FORMAT()
function formats a date according to a specified format, allowing for customized date output (e.g. displaying the full day name, month name etc). This function is excellent for improving report readability.
Query:
SELECT sale_id, product_name,
DATE_FORMAT(sale_date, '%W, %M %d, %Y')
AS formatted_sale_date FROM sales;
Output
sale_id | product_name | formatted_sale_date |
---|---|---|
1 | Widget A | Thursday, August 01, 2024 |
2 | Widget B | Monday, August 05, 2024 |
3 | Widget C | Wednesday, August 07, 2024 |
4 | Widget A | Saturday, August 10, 2024 |
5 | Widget B | Thursday, August 15, 2024 |
6 | Widget C | Tuesday, August 20, 2024 |
10. ADDDATE()
The ADDDATE()
function adds a specified time interval to a date. It is useful for calculating future or past dates based on a given date.
Query:
SELECT sale_id, product_name,
ADDDATE(sale_date, INTERVAL 10 DAY)
AS sale_date_plus_10_days
FROM sales;
Output
sale_id | product_name | sale_date_plus_10_days |
---|---|---|
1 | Widget A | 2024-08-11 |
2 | Widget B | 2024-08-15 |
3 | Widget C | 2024-08-17 |
4 | Widget A | 2024-08-20 |
5 | Widget B | 2024-08-25 |
6 | Widget C | 2024-08-30 |
11. ADDTIME()
The ADDTIME()
function adds a specified time interval to a time or datetime value. It is useful for adjusting times by adding hours, minutes or seconds.
Query:
SELECT sale_id, product_name, ADDTIME('10:30:00', '02:30:00') AS sale_time_plus_2hrs_30min FROM sales;
Output
sale_id | product_name | sale_time_plus_2hrs_30min |
---|---|---|
1 | Widget A | 13:00:00 |
2 | Widget B | 13:00:00 |
3 | Widget C | 13:00:00 |
4 | Widget A | 13:00:00 |
5 | Widget B | 13:00:00 |
6 | Widget C | 13:00:00 |
Conclusion
SQL Date Functions are powerful tools for managing and analysing temporal data. They streamline operations ranging from simple date extractions to complex interval calculations. Mastering these functions empowers users to derive meaningful insights and create accurate, dynamic reports. By integrating these functions into our queries, we can enhance both efficiency and clarity in handling date and time data.
FAQs
What are date functions in SQL?
Date functions in SQL are built-in functions designed to perform operations on date and time values. They allow users to manipulate, retrieve, and format dates and times, facilitating tasks such as calculating durations, extracting specific date parts, and adjusting dates for various analyses.
How to use date in SQL statement?
To use date functions in an SQL statement, you typically include the function within a
SELECT
,WHERE
, orORDER BY
clause to perform operations on date columns. For example,SELECT DATE(sale_date) FROM sales;
extracts only the date part from a datetime column, whileSELECT DATE_ADD(sale_date, INTERVAL 10 DAY) FROM sales;
adds 10 days to each date.
What is the date format function in SQL?
The
DATE_FORMAT()
function in SQL formats a date value according to a specified format string. It allows users to display dates in various formats, such as'%Y-%m-%d'
for ‘2024-08-12’ or'%M %d, %Y'
for ‘August 12, 2024’. This function is useful for customizing how dates are presented in query results.