Dhamo
Dhamo

Reputation: 65

How to get values based on specific DateTime data in SQL Server?

I have table called MachineDetails.

Table format as below:

S.No    Machine         StartTime                       EndTime
-------------------------------------------------------------------------
1       Machine-2   2017-04-27 06:07:00.000     2017-04-27 07:38:48.000
1       Machine-2   2017-04-27 11:12:48.000     2017-04-27 12:13:08.000
2       Machine-3   2017-04-27 08:56:35.000     2017-04-27 08:53:11.000
2       Machine-3   2017-04-27 09:26:40.000     2017-04-27 10:36:38.000
3       Machine-4   2017-04-27 16:01:26.000     2017-04-27 17:09:13.000
4       Machine-7   2017-04-27 23:42:57.000     2017-04-28 07:40:43.000 
5       Machine-8   2017-04-28 06:36:34.000     2017-04-28 08:21:07.000

I would like to filter data based on starttime and endtime.

E.g: I need output like as below:

S.No    Machine         StartTime                       EndTime
-------------------------------------------------------------------------
2       Machine-3   2017-04-27 08:56:35.000     2017-04-27 08:53:11.000
7       Machine-3   2017-04-27 09:26:40.000     2017-04-27 10:36:38.000

I tried this query but it is not working:

select * 
from MachineDetails 
where starttime >= '2017-04-28 08:00:00 AM' 
  and EndTime <= '2017-04-28 11:00:00 AM'

Can anyone please help me to solve this?

Upvotes: 0

Views: 12

Answers (1)

murtazat
murtazat

Reputation: 419

Looking at results you are expecting, it looks like your query itself is wrong. Shouldn't it be like the one given below? (Use 27 April)

select * from MachineDetails where starttime >= '2017-04-27 08:00:00 AM' and EndTime <= '2017-04-27 11:00:00 AM'

Upvotes: 1

Related Questions