๐ฅ Now, letโs move to the next topic
SQL Date & Time Functions โ
๐ง 1. Why Date Functions Matter?
Almost every real-world database contains dates ๐
โ Orders
โ Employee joining dates
โ Transactions
โ Login activity
SQL date functions help analyze time-based data ๐ฏ
โก 2. Common Date Functions
Function : Purpose
NOW() : Current date & time
CURDATE() : Current date
CURTIME() : Current time
YEAR() : Extract year
MONTH() : Extract month
DAY() : Extract day
DATEDIFF() : Difference between dates
DATE_FORMAT() : Format dates
๐ฅ 3. NOW(), CURDATE(), CURTIME()
SELECT NOW();
โ Current date + time
SELECT CURDATE();
โ Current date only
SELECT CURTIME();
โ Current time only
๐ฅ 4. YEAR(), MONTH(), DAY()
SELECT YEAR(joining_date)
FROM employees;
SELECT MONTH(joining_date)
FROM employees;
SELECT DAY(joining_date)
FROM employees;
๐ฅ 5. DATEDIFF()
๐ Find difference between dates
SELECT DATEDIFF('2026-06-10', '2026-06-01');
โ Result โ 9 days
๐ฅ 6. DATE_FORMAT()
๐ Format dates professionally
SELECT DATE_FORMAT(NOW(), '%d-%m-%Y');
โ Example Output โ 06-06-2026
๐ฏ 7. Real Example
๐ Employees joined in 2025
SELECT *
FROM employees
WHERE YEAR(joining_date) = 2025;
๐ฏ 8. Practice Tasks
1. Show current date
2. Show current time
3. Extract joining year from employee table
4. Find employees joined in specific month
5. Calculate days between two dates
โก Mini Challenge ๐ฅ
๐ Find employees who joined in the last 30 days
โ
Solution Using DATEDIFF()
SELECT *
FROM employees
WHERE DATEDIFF(CURDATE(), joining_date) <= 30;
โ
Alternative Solution Using DATE_SUB()
SELECT *
FROM employees
WHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
๐ฅ Pro Tip
Date functions are heavily used in:
๐ Sales reports
๐ Monthly dashboards
๐ Retention analysis
๐ Time-series analytics ๐ฏ
Double Tap โค๏ธ For More
Post #2495
2.53K
- โค 11