Recently, I was working on a requirement to calculate time differences in hours for one of my clients, and after executing a select query, I encountered the error ‘Operand data type datetime2 is invalid for the subtract operator.’ In this article, I will walk you through the cause of this error and the respective solution.
Operand data type datetime2 is invalid for subtract operator.
I was trying to execute the below SQL query.
SELECT EndTime - StartTime
FROM RunDetails
WHERE ID = 1;After executing the above query, I got this error as shown in the screenshot below.

What Causes This Error?
The error occurs because SQL Server doesn’t allow direct subtraction of datetime2 data types using the minus (-) operator.
Solution -1: Using the DATEDIFF Function
The most common and recommended method for calculating the difference between two datetime2 values is to use the DATEDIFF function.
Syntax:
DATEDIFF(datepart, startdate, enddate)Where:
- datepart: The unit you want the difference in (second, minute, hour, day, month, year, etc.)
- startdate: The earlier datetime value
- enddate: The later datetime value
We can use the below query to calculate the hour differences.
SELECT DATEDIFF(hour, StartTime, EndTime) AS HoursDifference
FROM RunDetails
WHERE ID = 1;After executing the above query, I got the expected output without any error as shown in the screenshot below.

Solution – 2: Using DATEDIFF_BIG for Larger Intervals
If you’re working with very large time differences that might exceed the range of an integer (2,147,483,647), use DATEDIFF_BIG instead:
SELECT DATEDIFF_BIG(second, StartTime, EndTime) AS SecondsDifference
FROM RunDetails
WHERE ID = 1;After executing the above query, I got the expected output without any error as shown in the screenshot below.

You may also like the following articles.
- The data types datetime and date are incompatible in the subtract operator.
- Explicit conversion from data type datetime2 to float is not allowed.
- How to Calculate Age from Date of Birth in SQL Server
- The datepart millisecond is not supported by date function dateadd for data type date.
- String or binary data would be truncated in table
- SQL Server conversion failed when converting date and/or time from character string.
After working for more than 15 years in the Software field, especially in Microsoft technologies, I have decided to share my expert knowledge of SQL Server. Check out all the SQL Server and related database tutorials I have shared here. Most of the readers are from countries like the United States of America, the United Kingdom, New Zealand, Australia, Canada, etc. I am also a Microsoft MVP. Check out more here.