首页 > 解决方案 > 如果当前时间在两个字段之间匹配,如何获取行数?

问题描述

ID _从 _到 代码
1 08:00 11:00 4002138
2 15:00 17:00 4002138

示例 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)

示例 2

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

请告诉我我哪里错了?

标签: sqlsql-server

解决方案


如果您指的是当前时间,那么您可以使用:

where convert(time, getdate()) >= _from and
      convert(time, getdate()) <= _to

推荐阅读