Count how many times a time occurs within a date range

David H

I could not think of a good way to phrase this question to search properly if its already been asked.

I'm looking for a way in SQL 2008 R2 to count how many times 6pm occurs between two datetime values.

For example between '2017-04-17 19:00:00' and '2017-04-19 17:00:00' 6pm only occurs once even though the times span 3 different days.

Between '2017-04-17 18:00:00' and '2017-04-19 18:00:00' it occurs 3 times whilst also spanning 3 days.

Heres a really silly made up expression of what I want for illustration.

timecount(hh, 6, min(datefield), max(datefield))

Thank you

TriV

A simple query to count:

DECLARE @StartDate datetime = '2017-04-17 18:00:00'

DECLARE @EndDate datetime = '2017-04-19 18:00:00'

SELECT 
   CASE 
      WHEN CAST(@StartDate AS time) <= '18:00' AND CAST(@EndDate AS time) >= '18:00' 
             THEN datediff(day, @StartDate, @EndDate) + 1
      WHEN CAST(@StartDate AS time) <= '18:00' AND CAST(@EndDate AS time) < '18:00' 
             THEN datediff(day, @StartDate, @EndDate)      
      WHEN CAST(@StartDate AS time) > '18:00' AND CAST(@EndDate AS time) >= '18:00' 
             THEN datediff(day, @StartDate, @EndDate)
      ELSE datediff(day, @StartDate, @EndDate) - 1
   END AS TotalCount

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel - how to count how many times a word appears within a certain date range

From Dev

How to count how many times a string occurs

From Dev

How many times same date occurs

From Dev

Count how many times an object occurs in a list of a list within a DataFrame column

From Dev

Count how many times a value occurs in a range (based on the value in another column)

From Mysql

Count how many times a rows enter time is within the enter and exit times of all other rows

From Dev

How to check how many times a substring occurs within a string?

From PHP

Count how many times something was echoed in a range?

From Dev

R: Count how many times value has occured before within certain range of rows

From Dev

how to count how many times a string occurs in a column using pandas

From Dev

XQuery: How to count how many times a value occurs in sequence

From Dev

How to count how many times a number occurs in each column of a file?

From Dev

Numpy, How to count how many times a string occurs

From Dev

count how many times each item occurs in a dictionary

From Dev

Count how many times a value occurs across tables

From Dev

Count how many times specific element occurs in array

From Dev

Count how many times certain text combinations occurs in certain columns

From Dev

Python: Count how many times a word occurs in a file

From Dev

Count how many times a word occurs in a HashMap per key

From Dev

Count how many rows have date within date range of each row for each ID Pandas

From Dev

How can I get a count of times a value occurs by date in mysql

From Dev

How to check if a time is within a range with date

From Dev

Ruby: How to check if date and time are within a range

From Dev

How many times a date has occured in a Date range

From Dev

Count observations for each group within dynamic date-time range

From Dev

How to count items in Excel within a date range or without an end date

From Dev

How do I find how many days are within a date range that are within another date range in PHP?

From Dev

How to work out specific days and times within a date range

From Dev

how many times specific days occur in a date range in ruby?

Related Related

  1. 1

    Excel - how to count how many times a word appears within a certain date range

  2. 2

    How to count how many times a string occurs

  3. 3

    How many times same date occurs

  4. 4

    Count how many times an object occurs in a list of a list within a DataFrame column

  5. 5

    Count how many times a value occurs in a range (based on the value in another column)

  6. 6

    Count how many times a rows enter time is within the enter and exit times of all other rows

  7. 7

    How to check how many times a substring occurs within a string?

  8. 8

    Count how many times something was echoed in a range?

  9. 9

    R: Count how many times value has occured before within certain range of rows

  10. 10

    how to count how many times a string occurs in a column using pandas

  11. 11

    XQuery: How to count how many times a value occurs in sequence

  12. 12

    How to count how many times a number occurs in each column of a file?

  13. 13

    Numpy, How to count how many times a string occurs

  14. 14

    count how many times each item occurs in a dictionary

  15. 15

    Count how many times a value occurs across tables

  16. 16

    Count how many times specific element occurs in array

  17. 17

    Count how many times certain text combinations occurs in certain columns

  18. 18

    Python: Count how many times a word occurs in a file

  19. 19

    Count how many times a word occurs in a HashMap per key

  20. 20

    Count how many rows have date within date range of each row for each ID Pandas

  21. 21

    How can I get a count of times a value occurs by date in mysql

  22. 22

    How to check if a time is within a range with date

  23. 23

    Ruby: How to check if date and time are within a range

  24. 24

    How many times a date has occured in a Date range

  25. 25

    Count observations for each group within dynamic date-time range

  26. 26

    How to count items in Excel within a date range or without an end date

  27. 27

    How do I find how many days are within a date range that are within another date range in PHP?

  28. 28

    How to work out specific days and times within a date range

  29. 29

    how many times specific days occur in a date range in ruby?

HotTag

Archive