How to Use Date Functions in SQL
In this post, I’ll show you how to use date functions in SQL with practical examples. When working with Microsoft SQL Server, you’ll often need to handle dates – whether it’s recording when an order was placed, calculating someone’s age, or finding out how many days have passed between two events. That’s where SQL’s date and time functions come in.
We’ll cover the basics of the DATE
data type, learn what does GETDATE return in SQL, explore how to use DATEADD in SQL, and even answer common questions like how to add 7 days to date in SQL or how to subtract 2 dates in SQL.
How to Use Date Data Type in SQL?
The DATE
data type is used to store just the date (year, month, and day) without the time portion. For example:
CREATE TABLE PurchaseOrders (
OrderID INT,
OrderDate DATE
)
When you insert a value:
INSERT INTO PurchaseOrders (OrderID, OrderDate)
VALUES (1, '2025-08-21')
SQL Server will store the date in YYYY-MM-DD
format by default:

What Does GETDATE Return in SQL?
The GETDATE()
function returns the current date and time according to your SQL Server system clock.
SELECT GETDATE() AS CurrentDateTime
If you run this, you’ll get something like:2025-08-27 16:24:54.940
This includes both the date and the exact time down to milliseconds:

How to Use DATEADD in SQL?
The DATEADD()
function lets you add or subtract intervals (days, months, years, etc.) from a date.
SELECT DATEADD(DAY, 30, GETDATE()) AS ThirtyDaysFromNow
This adds 30 days to today’s date:

How to Add 7 Days to Date in SQL?
A very common use case is adding exactly one week. With DATEADD()
, it’s simple:
SELECT DATEADD(DAY, 7, GETDATE()) AS OneWeekLater
This returns today’s date plus 7 days:

How to Use DATEDIFF in SQL?
The DATEDIFF()
function calculates the difference between two dates, returning the result in the unit you specify (days, months, years, etc.).
SELECT DATEDIFF(DAY, '2025-01-01', GETDATE()) AS DaysSinceNewYear
This tells you how many days have passed since January 1st, 2025:

How to Subtract 2 Dates in SQL?
To subtract dates, you don’t do it directly with -
. Instead, you use DATEDIFF()
.
Example:
SELECT
[OrderID]
,[CustomerID]
,DATEDIFF(DAY, OrderDate, ShippedDate) AS DaysToShip
FROM Orders
This shows how many days it took to ship each order.:

Final Thoughts
Learning how to use date functions in SQL is a must for working with real-world databases. Dates are everywhere (orders, logs, memberships, schedules…) and SQL gives you powerful tools to work with them.
In this post, you learned:
- How to use DATE data type in SQL to store dates.
- What does GETDATE return in SQL and how to format it.
- How to use DATEADD in SQL, including how to add 7 days to date in SQL.
- How to use DATEDIFF in SQL, including how to subtract 2 dates in SQL.
With these functions, you’ll be able to handle most of the date-related tasks in SQL Server.
If you want to find out more about SQL, make sure you check my previous blog post on How to Use String Functions in SQL. If you want to learn more about SQL date functions and data types, visit this link here.
If you would like to learn more about programming SQL queries, make sure you buy this book: T-SQL Fundamentals (Developer Reference)