In this detailed article, I’ll walk you through various methods for calculating the date difference in hours in SQL Server, providing practical examples and sharing tips on handling fractional hours and time zones. By the end, you’ll be confident handling any date difference calculation in SQL Server.
SQL Server Date Difference in Hours
Why Calculate Date Difference in Hours?
Calculating the difference between two dates in hours is a common requirement, such as:
- Healthcare: Tracking patient stay durations.
- Finance: Calculating interest accrual periods.
- Manufacturing: Monitoring machine uptime or downtime.
- Retail: Analyzing sales performance over specific hourly intervals.
Understanding the exact hours between two events is crucial for accurate reporting, billing, and operational optimization.
Prerequisites
Before diving into calculations, it’s essential to understand the standard date/time data types in SQL Server:
| Data Type | Description | Typical Use Case |
|---|---|---|
DATETIME | Date and time with 3.33 ms precision | Legacy systems, general purposes |
DATETIME2 | Date and time with higher precision | Newer applications needing accuracy |
SMALLDATETIME | Date and time with minute precision | Less precise time tracking |
DATE | Date only (no time) | Storing dates without time |
TIME | Time only | Storing time without date |
Most time difference calculations involve DATETIME or DATETIME2.
Method 1: Using DATEDIFF Function
The easiest and most commonly used method to calculate the difference in hours between two dates in SQL Server is the DATEDIFF function.
Syntax
DATEDIFF ( datepart , startdate , enddate )- datepart: The part of the date to calculate difference on (e.g., hour, minute).
- startdate: The beginning date/time.
- enddate: The ending date/time.
Example: Calculate Whole Hours Difference
SELECT DATEDIFF(hour, '2025-07-20 08:00:00', '2025-07-21 10:30:00') AS HoursDifference;After executing the above query, I got the expected output as shown in the screenshot below.

This counts the number of hour boundaries crossed between the two dates. Note that it returns an integer, so it does not account for fractional hours.
Method 2: Calculating Fractional Hours with DATEDIFF and DateTime Arithmetic
If you want the difference in decimal hours (including fractions), you need to calculate the difference in smaller units (such as seconds or minutes) and then convert it.
Step-by-Step
- Calculate the difference in seconds or minutes.
- Convert the result to hours by dividing by 3600 (seconds) or 60 (minutes).
Example: Using Seconds for Fractional Hours
SELECT
CAST(DATEDIFF(second, '2025-07-20 08:00:00', '2025-07-21 10:30:00') AS FLOAT) / 3600 AS FractionalHours;After executing the above query, I got the expected output as shown in the screenshot below.

This approach gives you the exact number of hours, including the fractional part.
Example: Using Minutes for Fractional Hours
SELECT
CAST(DATEDIFF(minute, '2025-07-20 08:00:00', '2025-07-21 10:30:00') AS FLOAT) / 60 AS FractionalHours;After executing the above query, I got the expected output as shown in the screenshot below.

Method 3: Using CAST and CONVERT
Sometimes, you might want to calculate the difference by subtracting datetime values directly and converting the result.
Example: Subtracting and Casting to FLOAT
In SQL Server, subtracting two datetime values returns a result in days (as a float). Multiply by 24 to get hours.
SELECT
CAST(CAST('2025-07-21 10:30:00' AS DATETIME2) - CAST('2025-07-20 08:00:00' AS DATETIME2) AS FLOAT) * 24 AS HoursDifference;Result: 26.5
This method is useful when working with DATETIME2 And when you want to avoid DATEDIFF’s integer limitation.
Check out Operand data type datetime2 is invalid for subtract operator.
Practical Examples
Example 1: Employee Shift Duration
Calculate how many hours an employee worked between clock-in and clock-out.
DECLARE @InTime DATETIME = '2025-07-21 09:15:00';
DECLARE @OutTime DATETIME = '2025-07-21 17:45:00';
SELECT
DATEDIFF(minute, @InTime, @OutTime) / 60.0 AS HoursWorked;After executing the above query, I obtained the expected output, which is 8.5 hours, as shown in the screenshot below.

Example 2: Machine Uptime Monitoring
Calculate machine uptime in hours from logs.
SELECT
MachineID,
CAST(DATEDIFF(second, StartTime, EndTime) AS FLOAT) / 3600 AS UptimeHours
FROM MachineLogs
WHERE MachineID = 101;
Summary Table of Methods
| Method | Precision | Pros | Cons |
|---|---|---|---|
DATEDIFF(hour, start, end) | Integer hours | Simple and fast | No fractional hours |
DATEDIFF(second/minute) / 3600 | Fractional hours | Accurate fractional calculations | Slightly more complex syntax |
| Subtracting datetime and multiplying by 24 | Fractional hours | Simple arithmetic, flexible | Requires casting, careful with types |
Using DATETIMEOFFSET and UTC conversion | Timezone aware | Accurate across time zones | More complex, requires timezone knowledge |
Conclusion
Calculating the difference between two dates in hours is essential for a database developer. You can use the simple DATEDIFF function, along with other methods mentioned in this article, to perform more precise fractional hour calculations.
Remember:
- Use
DATEDIFFfor quick integer hour differences. - Use seconds or minutes difference divided by 3600 or 60 for fractional hours.
- Consider time zones and daylight saving when working with US data.
You may also like following the articles below.
- Pivot Date Column In SQL Server
- Explicit conversion from data type datetime2 to float is not allowed.
- Convert dd/mm/yyyy to date SQL Server
- How to Calculate Age from Date of Birth in SQL Server
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.