Skip to content
Advertisement

How can I select data from my database in time intervals?

Suppose I have the following table in a database:

ID  | Date            | Data |
------------------------------
0   |2020-01-01 08:00 | 123
1   |2020-01-01 08:01 | 456
2   |2020-01-01 08:02 | 789
3   |2020-01-01 08:03 | 222
4   |2020-01-01 08:04 | 323
5   |2020-01-01 08:05 | 565
6   |2020-01-01 08:06 | 097
7   |2020-01-01 08:07 | 112
8   |2020-01-01 08:08 | 652
9   |2020-01-01 08:09 | 547
10  |2020-01-01 08:10 | 187

Is there a way to use a select statement to grab the data in intervals? For example, selecting data in 2 minutes intervals so that records 0,2,4,6,8, and 10 would show? Something like:

SELECT * FROM table_name
WHERE (Date is 2 minute intervals)
ORDER BY Date DESC

Advertisement

Answer

You need DATEPART() and modulo (%):

SELECT *
FROM Table_name
WHERE DATEPART(mi, [Date]) % 2 = 0

DEMO

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement