sql - 如果当前时间在两个字段之间匹配,如何获取行数?
问题描述
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))
请告诉我我哪里错了?
解决方案
如果您指的是当前时间,那么您可以使用:
where convert(time, getdate()) >= _from and
convert(time, getdate()) <= _to
推荐阅读
- c# - 为什么从字典中获取 Action<> 的克隆?
- less - Livereload 不会将源映射更新为更少的文件
- android - 仅通过受信任的 Android、iOS 和 Web 应用程序访问 Azure APIM
- javascript - 使用 puppeteer 配置 PDF 页面宽度
- .net-core - Dotnet CLI 命令无法运行 DLL
- android - 在 android 移动应用程序中使用带有用户名和密码的 url 登录
- c# - 将代码放在外部 dll 中时,SetWindowsHookEx 不起作用
- sql - 更新语句导致异常行为
- jquery - CypressError:重试超时:您试图对既不是 DOM 对象也不是 jQuery 对象的对象进行 chai-jQuery 断言
- php - 使用 Guzzle 发送请求后无法接收响应