đ Date Functions in SQL are built-in functions used to retrieve, manipulate, format, and calculate date and time values. They help developers perform operations such as finding the current date, adding days, calculating age, extracting months, and comparing dates.
đ What are Date Functions?
Date functions simplify working with temporal data. Instead of manually calculating dates, SQL provides functions to perform common operations quickly and accurately. Since SQL dialects differ, the names and syntax of some date functions vary between database systems.
Information
đ¯ Why Use Date Functions?
Date functions make it easier to query and analyze time-based data.
- đ Retrieve the current date and time.
- đ Add or subtract days, months, or years.
- đ Calculate the difference between dates.
- đ Extract individual date parts.
- đ Filter and sort records by date.
đ Sample Table
| OrderID | Customer | OrderDate | DeliveryDate |
|---|---|---|---|
| 101 | Alice | 2026-08-01 | 2026-08-05 |
| 102 | Bob | 2026-08-03 | 2026-08-08 |
| 103 | Charlie | 2026-08-05 | 2026-08-10 |
đ Get the Current Date and Time
Many SQL databases support standard functions for retrieving the current date and time.
Current Date and Time
SELECT
CURRENT_DATE,
CURRENT_TIME,
CURRENT_TIMESTAMP;â Add or Subtract Dates
Date arithmetic lets you calculate future or past dates. The exact function varies by database system.
SQL Server Example
SELECT DATEADD(day, 7, '2026-08-01') AS NextWeek;MySQL Example
SELECT DATE_ADD('2026-08-01', INTERVAL 7 DAY) AS NextWeek;đ Calculate Date Difference
You can determine the number of days, months, or years between two dates using database-specific functions.
SQL Server Example
SELECT DATEDIFF(day, '2026-08-01', '2026-08-10') AS DaysBetween;MySQL Example
SELECT DATEDIFF('2026-08-10', '2026-08-01') AS DaysBetween;đ Extract Date Parts
Extracting individual parts of a date is useful for reporting and grouping.
Extract Year, Month, and Day
SELECT
EXTRACT(YEAR FROM OrderDate) AS OrderYear,
EXTRACT(MONTH FROM OrderDate) AS OrderMonth,
EXTRACT(DAY FROM OrderDate) AS OrderDay
FROM Orders;Important
đ Common Date Functions
| Function | Purpose |
|---|---|
| CURRENT_DATE | Returns the current date. |
| CURRENT_TIME | Returns the current time. |
| CURRENT_TIMESTAMP | Returns the current date and time. |
| EXTRACT() | Extracts parts such as year or month. |
| DATEADD() / DATE_ADD() | Adds a time interval. |
| DATEDIFF() | Calculates the difference between two dates. |
đ Filter Records by Date
Orders in August 2026
SELECT *
FROM Orders
WHERE OrderDate >= '2026-08-01'
AND OrderDate < '2026-09-01';đ Group Records by Month
Monthly Orders
SELECT
EXTRACT(MONTH FROM OrderDate) AS Month,
COUNT(*) AS TotalOrders
FROM Orders
GROUP BY EXTRACT(MONTH FROM OrderDate);âī¸ Standard vs Database-Specific Functions
| Operation | ANSI SQL | Database-Specific Examples |
|---|---|---|
| Current Date | CURRENT_DATE | CURDATE() (MySQL) |
| Current Timestamp | CURRENT_TIMESTAMP | GETDATE() (SQL Server) |
| Extract Year | EXTRACT() | YEAR() (MySQL, SQL Server) |
| Add Days | Implementation varies. | DATEADD(), DATE_ADD() |
đŧ Real-World Examples
- đ Calculate estimated delivery dates.
- đ Generate monthly sales reports.
- đ Calculate customer age from birth dates.
- đ Find upcoming appointments.
- đĻ Measure shipping duration.
đī¸ Database Compatibility
| Database System | Date Function Support |
|---|---|
| MySQL | Extensive date functions including CURDATE(), DATE_ADD(), and DATEDIFF(). |
| PostgreSQL | Strong ANSI SQL support with EXTRACT() and interval arithmetic. |
| SQL Server | Supports GETDATE(), DATEADD(), DATEDIFF(), and more. |
| Oracle | Rich date arithmetic and formatting functions. |
| SQLite | Provides built-in date/time functions such as date(), datetime(), and strftime(). |
â ī¸ Common Mistakes
- â Assuming date function names are identical across databases.
- â Using ambiguous date formats.
- â Ignoring time zone considerations.
- â Performing manual string operations instead of using date functions.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ Date functions simplify working with temporal data.
- đ Use built-in functions to retrieve, calculate, and manipulate dates.
- đ Function names and syntax vary among SQL databases.
- đ Standard functions improve portability when available.
- đ Native date functions are more reliable than string manipulation.
- đ Understanding your DBMS's date functions is essential for accurate reporting and calculations.