SQL Server Date Math

In this comprehensive article, I’ll walk you through everything you need to know about manipulating dates in SQL Server – from basic operations to advanced techniques. Let’s dive in.

SQL Server Date Math

Before we explore the specifics, it’s essential to understand why date manipulation is so critical in database applications:

  • Business Intelligence: Accurate period-over-period comparisons drive decision making
  • Compliance: Many industries (healthcare, finance, etc.) have strict time-based record-keeping requirements
  • Performance: Properly formatted date operations can dramatically improve query speed
  • Reporting: Almost every business report includes some form of date-based calculation

Check out SQL Server Date Data Types

Getting the Current Date and Time in SQL Server

Let’s start with the basics – retrieving the current date and time:

Example 1

We can execute the following query to return the date and time with SQL Server’s default precision.

-- Returns date and time with SQL Server's default precision
SELECT GETDATE() AS CurrentDateTime;

After executing the above query, I obtained the expected output, as shown in the screenshot below.

SQL Server Date Math

Example 2

We can execute the following query to return the date and time with higher precision.

SELECT SYSDATETIME() AS CurrentDateTimeHighPrecision;

After executing the above query, I obtained the expected output, as shown in the screenshot below.

SQL Date Math

Example 3

We can execute the following query to return just the current date.

SELECT CONVERT(date, GETDATE()) AS CurrentDate;

Below is the expected output after executing the above query.

sql server date math subtract days

Example 4

We can execute the following query to return just the current time.

SELECT CONVERT(time, GETDATE()) AS CurrentTime;

Below is the expected result after executing the above query.

sql date math functions

Essential Date Math Operations in SQL Server

Now let’s explore the fundamental date math operations you’ll use regularly.

Adding and Subtracting Time Periods with DATEADD

The DATEADD Function is your primary function for adding or subtracting time intervals:

Example 1

Let us execute the following query to add 7 days to the current date.

SELECT DATEADD(day, 7, GETDATE()) AS OneWeekFromNow;

I got the expected output after executing the above query.

SQL Server Date Math operations

Example 2

We can execute the following query to subtract 3 months from the current date.

SELECT DATEADD(month, -3, GETDATE()) AS ThreeMonthsAgo;

Below is the expected result after executing the above query.

SQL Server Date Math calculations

Example 3

We can execute the following query to add 4 hours to the current time.

SELECT DATEADD(hour, 4, GETDATE()) AS FourHoursLater;

I obtained the expected output after executing the query as shown in the screenshot below.

Date Math Operations in SQL Server

Example 4

We can execute the following query to add 30 seconds.

SELECT DATEADD(second, 30, GETDATE()) AS ThirtySecondsLater;

Below is the expected result after executing the above query.

Essential Date Math Operations in SQL Server

DATEADD accepts the following interval types:

  • year, yyyy, yy
  • quarter, qq, q
  • month, mm, m
  • dayofyear, dy, y
  • day, dd, d
  • week, ww, wk
  • hour, hh
  • minute, mi, n
  • second, ss, s
  • millisecond, ms
  • microsecond, mcs
  • nanosecond, ns

Calculating Time Differences with DATEDIFF

The DATEDIFF Function calculates the difference between two dates:

Example 1

Let us execute the following query to find the days between two dates.

SELECT DATEDIFF(day, '2025-01-01', '2025-12-31') AS DaysInYear;

Below is the expected result after executing the above query.

Date Math Operations in SQL

Example 2

Let us execute the following query to find the months between two dates.

SELECT DATEDIFF(month, '2024-03-15', GETDATE()) AS MonthsSinceDate;

Below is the expected result after executing the above query.

Essential Date Math Operations in SQL

Example 3

Let us execute the following query to find the minutes between two times.

SELECT DATEDIFF(minute, '2025-05-04 09:30:00', '2025-05-04 11:45:00') AS MinutesDifference;

Below is the expected result after executing the above query.

Calculating Time Differences with DATEDIFF

Extracting Date Parts with DATEPART

To extract specific components from a date with DATEPART function:

Example 1

We can execute the following query to get the current month number (1-12).

SELECT DATEPART(month, GETDATE()) AS CurrentMonth;

After executing the above query, I got the expected output as per the screenshot below.

Extracting Date Parts with DATEPART

Example 2

We can execute the query below to retrieve the day of the week (1 = Sunday, 2 = Monday, etc.).

SELECT DATEPART(weekday, GETDATE()) AS DayOfWeek;

I got the expected output after executing the above query.

sql server date difference in days

Example 3

We can execute the below query to get the hour from a datetime.

SELECT DATEPART(hour, GETDATE()) AS CurrentHour;

I got the expected output after executing the above query as per the below screenshot.

Date Parts with DATEPART

Performance Considerations

Below are the performance tips

  1. Avoid functions on indexed columns– Bad (can’t use index) WHERE YEAR(OrderDate) = 2023 — Good (can use index) WHERE OrderDate >= ‘2023-01-01’ AND OrderDate < ‘2024-01-01’

2. Use appropriate data types– More efficient when you only need a date OrderDate date NOT NULL.

Conclusion

Date manipulation is so critical in database applications that you have to know as a database developer working with SQL Server. You can use the information mentioned in this article.

You may also like the following articles.