数据库表T_time
mark begintime endtime
0     8:00:00   16:00:00
1     16:00:00   0:00:00
2     0:00:00    8:00:00

比如我现在已知电脑时间为 9:00:00如何查出第一条记录?比如已知时间为17:00:00,如何查出第二条记录??
select * from t_time where begintime <= '9:00:00 ' and endtime > '9:00:00 '
或 select * from t_time  '9:00:00 ' between begintime and endtime 都不可以呀,怎么整??

----

处理不包含日期的时间段declare @T_time table(mark int,begintime datetime,endtime datetime)
处理不包含日期的时间段
insert @T_time
处理不包含日期的时间段
select 0,'8:00:00','16:00:00' union all
处理不包含日期的时间段
select 1,'16:00:00','0:00:00' union all
处理不包含日期的时间段
select 2,'0:00:00','8:00:00'
处理不包含日期的时间段
处理不包含日期的时间段
select begintime,
处理不包含日期的时间段    endtime
=case when datediff(day,begintime,endtime)>=1
处理不包含日期的时间段        
then dateadd(d,-1,endtime)
处理不包含日期的时间段        
else endtime
处理不包含日期的时间段    
end
处理不包含日期的时间段
from 
处理不包含日期的时间段(    
select 
处理不包含日期的时间段        begintime, 
处理不包含日期的时间段        endtime
=case when endtime<begintime 
处理不包含日期的时间段        
then dateadd(d,1,endtime)
处理不包含日期的时间段        
else endtime
处理不包含日期的时间段    
end 
处理不包含日期的时间段    
from @T_time ) b 
处理不包含日期的时间段
where '18:0:0' between b.begintime and b.endtime

处理不包含日期的时间段--假设begintime,endtime为字符型 
处理不包含日期的时间段
select * from tb where convert(varchar(8),getdate(),114)  >= begintime and convert(varchar(8),getdate(),114)  <= endtime

相关文章:

  • 2022-12-23
  • 2021-11-28
  • 2021-04-29
  • 2021-05-14
  • 2021-08-04
  • 2021-09-29
  • 2021-07-09
  • 2022-01-07
猜你喜欢
  • 2022-01-04
  • 2022-12-23
  • 2021-06-29
  • 2021-06-08
  • 2021-12-05
  • 2022-12-23
相关资源
相似解决方案