Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i want to filter time i have records
i wrote query like this query will fetch time between 12:00 Pm and 05:00 Pm
Select cs_ClinicName , FromTime ,ToTime from #temp
WHERE (1=1) and
( FromTime between CONVERT(Time,'12:00') and CONVERT(Time,'17:00')
or
ToTime between CONVERT(Time,'12:00') and CONVERT(Time,'17:00') )

But it is filtering only this records <pre lang="css">
Micolaj11111 09:00:00.0000000 13:00:00.0000000

what i a m expecting is i t must give all clinic who work between
<pre lang="css">between 12:00 Pm and 05:00 Pm
according to the records all clinic work between that time slot(<pre lang="css">12:00 Pm and 05:00 Pm)
Clinic NAME                     FromTime             ToTime
Boyka                          09:00:00.0000000    18:00:00.0000000
Clinic Name                    10:00:00.0000000    18:00:00.0000000
sleek clini                    10:00:00.0000000    18:00:00.0000000
Life Clinic                    10:00:00.0000000    18:00:00.0000000
SAiClini                       09:00:00.0000000    18:00:00.0000000
jh                             10:00:00.0000000    19:00:00.0000000
Micolaj11111                09:00:00.0000000    13:00:00.0000000
Dental Clinic                  10:00:00.0000000    19:00:00.0000000
Eye clinic                     09:00:00.0000000    18:00:00.0000000
ryan                           01:00:00.0000000     06:00:00.0000000
<pre>
Posted
Updated 17-Oct-13 0:22am
v5

1 solution

The result is returning correctly as there is only on record between 12:00 to 17:00. If you want if the clinic is open between 12:00 to 17:00 then try this query. Replace @a with your table name.


SQL
Select  CONVERT(Time,'12:00'),CONVERT(Time,'17:00'),ClinicName , FromTime ,ToTime from @a
 WHERE (1=1) and
 (( FromTime <= CONVERT(Time,'12:00') and ToTime >= CONVERT(Time,'12:00'))
 or
 (FromTime <= CONVERT(Time,'17:00') and ToTime >= CONVERT(Time,'17:00') )
 
Share this answer
 
v2
Comments
surajemo 17-Oct-13 6:57am    
i tried your query The above query will show all records except Micolaj11111 09:00:00.0000000 13:00:00.0000000 – Even Micolaj11111 clinic works between 12:00 Pm and 5 pm :)
ArunRajendra 17-Oct-13 8:17am    
That's because Micolaj11111 works only between 9 to 1 (13:00) and not from 12 to 5:00 (17:00). If you want even Micolaj11111 then change the condition to ( FromTime <= CONVERT(Time,'12:00') or ToTime >= CONVERT(Time,'17:00') )
ArunRajendra 17-Oct-13 23:04pm    
Check the modified solution.
surajemo 17-Oct-13 8:56am    
i had posted the similar question in another forums they are replied like this

00:00 - 08:59

09:00 - 11:59

12:00 - 4:59

even more accurate use can do the same for seconds too...

declare @fromtime time ='00:00' ,@totime time = '08:59'
select * from
(
select 'Boyka' as Clinic_NAME, '09:00:00.0000000' as FromTime ,'18:00:00.0000000' as ToTime union all
select 'Clinic Name', '10:00:00.0000000' ,'18:00:00.0000000' union all
select 'sleek clini', '10:00:00.0000000' ,'18:00:00.0000000' union all
select 'Life Clinic', '10:00:00.0000000' ,'18:00:00.0000000' union all
select 'SAiClini', '09:00:00.0000000' ,'18:00:00.0000000' union all
select 'jh', '10:00:00.0000000' ,'19:00:00.0000000' union all
select 'Micolaj11111', '09:00:00.0000000' ,'13:00:00.0000000' union all
select 'Dental Clinic-', '10:00:00.0000000' ,'19:00:00.0000000' union all
select 'Eye clinic', '09:00:00.0000000' ,'18:00:00.0000000' union all
select 'ryan' , '01:00:00.0000000' ,'06:00:00.0000000' ) a
where convert(time,fromTime) between CONVERT(Time,@fromtime) and CONVERT(Time,@totime)
or convert(time,ToTime) between CONVERT(Time,@fromtime) and CONVERT(Time,@totime)
or (convert(time,fromTime) <= CONVERT(Time,@fromtime) and convert(time,toTime) > CONVERT(Time,@totime) )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900