Date Functions
Work with dates — extract, calculate, format
✅ What You Will Learn
Date functions extract or calculate date-related values — the year, month, or day from a date column, the difference between two dates, or the current date and time. Date analysis is central to almost every analytics project: trend analysis, cohort analysis, aging reports, and time-series all rely on date functions.
📋 The orders table — date functions extract parts of order_date
| order_id | customer_name | product | amount | order_date |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | 2026-01-15 |
| 1002 | Priya Verma | Mobile Phone | 18000 | 2026-01-16 |
| 1003 | Amit Kumar | Headphones | 3500 | 2026-02-03 |
| 1004 | Sneha Kapoor | Laptop | 52000 | 2026-02-17 |
| 1005 | Vikram Singh | Tablet | 28000 | 2026-03-05 |
Syntax
Examples
📌 Key Points to Remember
- ✓YEAR(), MONTH(), DAY() extract parts from a date — useful for grouping
- ✓GROUP BY YEAR() and MONTH() together for monthly trend reports
- ✓DATEDIFF syntax differs between MySQL and SQL Server — check your database
- ✓Always filter dates as date values, not as text strings
- ✓Date functions are essential for trend analysis, aging reports, and cohort analysis
🏢 Real-World Application
Date functions are essential for any time-series reporting. Monthly revenue reports use MONTH(order_date) and YEAR(order_date) for GROUP BY. Customer age calculation for KYC uses DATEDIFF (today, date_of_birth). Reports showing "orders in last 30 days" filter with WHERE order_date >= DATEADD(DAY, -30, GETDATE()). Cohort analysis groups users by signup month using DATEPART(MONTH, signup_date). Every analytics role deals with dates constantly — quarterly reports, year-over-year comparisons, and rolling 7-day or 30-day metrics all rely on date functions.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
How do I get the current date in SQL?
It varies by database: GETDATE() in SQL Server, NOW() or CURDATE() in MySQL, CURRENT_DATE in PostgreSQL and standard SQL. GETDATE() returns date + time; CURDATE() returns only the date portion.
How do I extract the month and year from a date in SQL?
In SQL Server and MySQL: YEAR(date_column) and MONTH(date_column). In PostgreSQL: EXTRACT(YEAR FROM date_column) and EXTRACT(MONTH FROM date_column). GROUP BY YEAR(order_date), MONTH(order_date) is a standard monthly report pattern.
How do I calculate the number of days between two dates?
In SQL Server: DATEDIFF(DAY, start_date, end_date). In MySQL: DATEDIFF(end_date, start_date). In PostgreSQL: end_date - start_date (date subtraction returns an integer number of days).
How do I filter records from the last 30 days in SQL?
SQL Server: WHERE order_date >= DATEADD(DAY, -30, GETDATE()). MySQL: WHERE order_date >= DATE_SUB(NOW(), INTERVAL 30 DAY). PostgreSQL: WHERE order_date >= CURRENT_DATE - INTERVAL '30 days'.
How do I format a date for display in SQL?
SQL Server: FORMAT(date_col, 'dd/MM/yyyy'). MySQL: DATE_FORMAT(date_col, '%d/%m/%Y'). PostgreSQL: TO_CHAR(date_col, 'DD/MM/YYYY'). Formatting is for display only and should be applied at the SELECT stage, not in WHERE or GROUP BY.
✏️ Practice Exercise
Write a query to find the total revenue and number of orders for each month in 2026, sorted by month.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.