Skip to content
Advertisement

How to get the number of lines if the current times matches between two fields?

ID _From _To Code
1 08:00 11:00 4002138
2 15:00 17:00 4002138

Example 1

select COUNT(ID) from GroupsTime where code='4002138' and
CAST(REPLACE(convert(nvarchar(5), cast(getdate() as time),120),':','') as integer) 
between CAST(REPLACE(convert(nvarchar(5), cast(_To as time),120),':','') as integer) and 
CAST(REPLACE(convert(nvarchar(5), cast(_From as time),120),':','') as integer)

Example 2

select COUNT(*) from GroupsTime where code='4002138' 
and (_From>convert(time,getdate(),120) or _To<convert(time,getdate(),120))

Please tell me where am i wrong?

Advertisement

Answer

If you are referring to the current time, then you can use:

where convert(time, getdate()) >= _from and
      convert(time, getdate()) <= _to
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement