Physical Address
Haryana ,India
Physical Address
Haryana ,India
In 2025, data analytics is all about timing — literally. Every click, purchase, signup, or event is tied to a timestamp. For analysts working with MySQL, date and time functions are essential tools to transform raw timestamps into business insights.
Mastering these functions means you can answer questions like:
If you want to level up your analytics game this year, date and time functions are a must-know skill.
In analytics, dates aren’t just labels — they’re filters, grouping keys, and calculation inputs. A few examples where these functions shine:
Without them, your SQL queries would be clunky and error-prone.
You must understand how dates are stored before you may alter them.
Data Type | Example | Notes |
DATE | 2025-08-11 | Stores only date |
DATETIME | 2025-08-11 15:30:00 | Date + time, no timezone |
TIMESTAMP | 2025-08-11 15:30:00 | Date + time, timezone aware |
TIME | 15:30:00 | Stores time only |
YEAR | 2025 | Year only |
SELECT CURDATE(); -- 2025-08-11
SELECT NOW(); -- 2025-08-11 15:45:22
SELECT CURRENT_TIMESTAMP();
Example: Get today’s orders
SELECT * FROM orders WHERE DATE(order_date) = CURDATE();
SELECT YEAR(order_date) AS year FROM orders;
SELECT MONTH(order_date) AS month_num, MONTHNAME(order_date) AS month_name FROM orders;
SELECT DAY(order_date), DAYNAME(order_date) FROM orders;
SELECT WEEK(order_date) AS week_num FROM orders;
Shifting time periods is common in rolling reports.
SELECT DATE_ADD('2025-08-11', INTERVAL 15 DAY);
SELECT DATE_SUB('2025-08-11', INTERVAL 1 MONTH);
Example: Last 90 days sales
SELECT * FROM sales
WHERE sale_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND CURDATE();
SELECT DATEDIFF('2025-08-15', '2025-08-10'); -- 5
SELECT TIMESTAMPDIFF(HOUR, '2025-08-10 10:00:00', '2025-08-11 14:00:00'); -- 28
SELECT DATE_FORMAT(order_date, '%M %d, %Y') AS formatted FROM orders;
SELECT TIME_FORMAT(order_time, '%h:%i %p') FROM orders;
MySQL 8.0.28+ gives us DATE_TRUNC():
SELECT DATE_TRUNC('month', order_date) AS month_start FROM orders;
Perfect for grouping data by fixed time periods.
SELECT CONVERT_TZ('2025-08-11 14:00:00', 'UTC', 'Asia/Kolkata');
This ensures accuracy for global datasets.
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(amount) AS total_sales
FROM orders
GROUP BY month
ORDER BY month;
SELECT YEAR(signup_date) AS year, MONTH(signup_date) AS month, COUNT(*) AS users
FROM customers
GROUP BY year, month;
SELECT order_id, TIMESTAMPDIFF(HOUR, order_date, shipped_date) AS hours_to_ship
FROM orders;
SELECT WEEK(order_date) AS week_num, SUM(amount) AS total_sales
FROM sales
GROUP BY week_num;
In 2025, MySQL’s date and time functions are more than convenience tools — they’re analytics power-ups. From extracting precise time intervals to formatting and grouping for reports, these functions help analysts move from raw timestamps to clear, actionable insights.
The better you master them, the more valuable you’ll be as a data analyst.
Q1: In MySQL, which function yields just the current date?
A1: CURDATE().
Q2: How do I group sales by quarter?
A2: Use QUARTER(order_date).
Q3: How to get time difference in minutes?
A3: TIMESTAMPDIFF(MINUTE, start_time, end_time).
Q4: Can I use DATE_TRUNC() in MySQL 5.7?
A4: No, it’s available only from MySQL 8.0.28+.
Q5: Q5: How can local time be converted from UTC time?
A5: Use CONVERT_TZ(datetime, ‘UTC’, ‘Your_Timezone’).