Junior Developer
Junior Developer

Reputation: 11

Find Time Period Count Between Specific Dates

How can we find count of time period (etc. 12:00 PM - 01:00 PM) between two datetime values in T-Sql?

For example;

We choose "10/07/2015 12:00 AM" as start datetime (this value is parametrical) and we choose "10/09/2015 12:00 AM" as end datetime (this value is parametrical)

We want to learn count of time period (12:00 PM - 01:00 PM).

In this example;

10/07/2015 12:00 PM - 10/07/2015 01:00 PM (First)

10/08/2015 12:00 PM - 10/08/2015 01:00 PM (Second)

And we have to get "two".

Can you help about this?

Upvotes: 0

Views: 47

Answers (1)

Abdullah Nehir
Abdullah Nehir

Reputation: 1047

declare @a datetime = '2015-01-01 12:00:00'
declare @b datetime = '2015-02-01 13:00:00'

select datediff(HOUR, @a, @b)

You can check the interval types you can use with datediff(...) function: https://msdn.microsoft.com/en-us/library/ms189794.aspx

Upvotes: 1

Related Questions