![]() |
VOOZH | about |
SQL Date Functions are built-in tools used to handle, modify and analyze date/time values in a database. They help perform tasks like retrieving current dates, calculating differences and formatting results effectively.
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:
The NOW() function retrieves the serverβs current date and time, making it useful for capturing exact event moments such as transaction timestamps, as well as for logging and comparing time-based records.
Query:
SELECT NOW() AS current_datetime;Output:
The CURDATE() function returns todayβs date in YYYY-MM-DD format and is useful when only the current date is needed, especially for reporting or filtering records by date.
Query:
SELECT CURDATE() AS current_date;Output:
The CURTIME() function returns the current time in HH:MM:SS format and is useful for time-based operations, such as scheduling or performing precise time comparisons.
Query:
SELECT CURTIME() AS current_time;Output:
The DATE() function extracts only the date from a date or datetime value, making it useful for situations where the time component should be ignored, such as date-only comparisons or aggregations.
Query:
SELECT
sale_id,
product_name,
DATE(sale_date) AS sale_date_only
FROM sales;
Output
The EXTRACT() function retrieves a specific part of a date such as the year, month or day, making it useful for grouping, filtering or performing time-based analysis including year-over-year reports.
Query:
SELECT sale_id, product_name,
EXTRACT(YEAR FROM sale_date)
AS sale_year FROM sales;
Output:
DATE_ADD()The DATE_ADD() function adds a chosen time interval such as days, months or years to a date, making it useful for calculating future dates and simplifying planning or scheduling tasks.
Query:
SELECT sale_id, product_name,
DATE_ADD(sale_date, INTERVAL 7 DAY) AS sale_date_plus_7_days
FROM sales;
Output:
The DATE_SUB() function subtracts a chosen time interval from a date, making it useful for determining past dates and performing 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:
The DATEDIFF() function returns the number of days between two dates, making it useful for calculating durations such as deadlines or overdue periods. In this case, it shows how many days remain from each sale date until August 15, 2024.
Query:
SELECT sale_id, product_name, sale_date,
DATEDIFF('2024-08-15', sale_date) AS days_until_aug15
FROM sales;
Output:
The DATE_FORMAT() function formats a date using a specified pattern, allowing customized output such as full day or month names and is useful for making reports clearer and more readable.
Query:
SELECT sale_id, product_name,
DATE_FORMAT(sale_date, '%W, %M %d, %Y')
AS formatted_sale_date FROM sales;
Output:
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, 10)
AS sale_date_plus_10_days
FROM sales;
Output:
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: