数据库表有begintime endtime两个时间类型字段,取现在任一时间,查询数据库记录问题??
数据库表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
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
select * from tb where convert(varchar(8),getdate(),114) >= begintime and convert(varchar(8),getdate(),114) <= endtime