Data types and expressions
Date and time functions
Calculate dates and date-part boundaries from stored dates.
Lesson 25 of 84
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page4 sections
This lesson uses the unmodified DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Calculate dates and date-part boundaries from stored dates.
Run the example
sql
SELECT TOP (3) OrderID, OrderDate,
DATEADD(day, 7, OrderDate) AS FollowUpDate
FROM dbo.Orders
ORDER BY OrderID;Read it step by step
- OrderDate is a date, not a timestamp.
- DATEADD(day, 7, OrderDate) calculates a date seven days later.
- ORDER BY OrderID makes the selected first three orders repeatable.
Check the result
| OrderID | OrderDate | FollowUpDate |
|---|---|---|
| 1 | 2023-01-02 | 2023-01-09 |
| 2 | 2023-01-03 | 2023-01-10 |
| 3 | 2023-01-04 | 2023-01-11 |
FollowUpDate is calculated at query time; it is not a stored column.
Practice
Show the month-end date for Orders 1 through 3 using EOMONTH.
Check your answer and expected result
Reveal answer
sql
SELECT OrderID, EOMONTH(OrderDate) AS MonthEnd
FROM dbo.Orders
WHERE OrderID BETWEEN 1 AND 3
ORDER BY OrderID;| OrderID | MonthEnd |
|---|---|
| 1 | 2023-01-31 |
| 2 | 2023-01-31 |
| 3 | 2023-01-31 |
All three orders fall in January 2023, so each MonthEnd is 2023-01-31.
Your progress
Reveal the check answer or reach the end to complete this lesson.