What if you could skip the headache of counting days and let the computer do it perfectly every time?
Why DATE arithmetic (DATEDIFF, DATE_ADD) in SQL? - Purpose & Use Cases
Imagine you have a paper calendar and you need to find out how many days are between two dates or what date it will be 10 days from today. Doing this by flipping pages and counting manually is slow and confusing.
Manually calculating dates can lead to mistakes, especially with different month lengths, leap years, and time zones. It takes a lot of time and is easy to get wrong.
DATE arithmetic functions like DATEDIFF and DATE_ADD let you quickly and accurately calculate the difference between dates or add days to a date with simple commands, removing all the guesswork.
Count days by hand on calendar Add days by guessing month lengths
SELECT DATEDIFF('2024-06-20', '2024-06-10'); SELECT DATE_ADD('2024-06-10', INTERVAL 10 DAY);
You can easily calculate durations and future or past dates in your data, making scheduling and reporting fast and reliable.
A company wants to know how many days passed since a customer placed an order or when a subscription will expire by adding 30 days to the start date.
Manual date calculations are slow and error-prone.
DATE arithmetic functions automate and simplify date math.
This makes working with dates in databases fast, accurate, and easy.
Practice
DATEDIFF('2024-06-10', '2024-06-01') return?Solution
Step 1: Understand DATEDIFF parameters
DATEDIFF calculates the number of days between the first date and the second date.Step 2: Calculate the difference
From June 1 to June 10, there are 9 days difference (10 - 1 = 9).Final Answer:
9 -> Option DQuick Check:
DATEDIFF('2024-06-10', '2024-06-01') = 9 [OK]
- Counting both start and end dates
- Mixing order of dates in DATEDIFF
- Assuming difference is 10 instead of 9
Solution
Step 1: Recall DATE_ADD syntax
The correct syntax is DATE_ADD(date, INTERVAL number unit), e.g., INTERVAL 5 DAY.Step 2: Check each option
Only DATE_ADD('2024-06-01', INTERVAL 5 DAY) follows the correct syntax with INTERVAL 5 DAY.Final Answer:
DATE_ADD('2024-06-01', INTERVAL 5 DAY) -> Option AQuick Check:
Correct DATE_ADD syntax uses INTERVAL before number and unit [OK]
- Omitting INTERVAL keyword
- Swapping number and unit order
- Placing INTERVAL after unit
SELECT DATEDIFF('2024-07-01', DATE_ADD('2024-06-25', INTERVAL 3 DAY)) AS diff;Solution
Step 1: Calculate DATE_ADD('2024-06-25', INTERVAL 3 DAY)
Adding 3 days to June 25 gives June 28, 2024.Step 2: Calculate DATEDIFF('2024-07-01', '2024-06-28')
The difference between July 1 and June 28 is 3 days (June 29, 30, July 1).Final Answer:
3 -> Option BQuick Check:
DATEDIFF('2024-07-01', '2024-06-28') = 3 [OK]
- Counting days incorrectly between dates
- Forgetting to add days before DATEDIFF
- Assuming DATEDIFF counts inclusive days
SELECT DATE_ADD('2024-06-01', 5 DAY);Solution
Step 1: Review DATE_ADD syntax
DATE_ADD requires INTERVAL keyword before the number and unit.Step 2: Check the given statement
The statement misses INTERVAL before '5 DAY', causing syntax error.Final Answer:
Missing INTERVAL keyword before 5 DAY -> Option CQuick Check:
DATE_ADD needs INTERVAL before number and unit [OK]
- Omitting INTERVAL keyword
- Using wrong date format
- Adding semicolon inside function
Solution
Step 1: Calculate deadline date
Deadline is 10 days after June 15, so DATE_ADD('2024-06-15', INTERVAL 10 DAY) = June 25.Step 2: Calculate days left from June 20 to deadline
DATEDIFF(deadline, current_date) = DATEDIFF('2024-06-25', '2024-06-20') = 5 days left.Final Answer:
SELECT DATEDIFF(DATE_ADD('2024-06-15', INTERVAL 10 DAY), '2024-06-20') AS days_left; -> Option AQuick Check:
Days left = deadline - current date = 5 [OK]
- Swapping date order in DATEDIFF
- Adding interval to wrong date
- Using wrong syntax for INTERVAL
Ready to prove it?
Pass the test and this pattern moves unknown
