日期:2014-05-18  浏览次数:20487 次

数据库表有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 都不可以呀,怎么整??

------解决方案--------------------
假设begintime,endtime为字符型
select * from tb where convert(varchar(8),getdate(),114) >= begintime and convert(varchar(8),getdate(),114) <= endtime
------解决方案--------------------
俺都2005,也用过2000,没见过可视化之后把datetime的日期部分给不可视了:

SQL code
SELECT *
FROM t_time 
WHERE (convert(varchar,GETDATE(),114) > convert(varchar,BEGINTIME,114) AND (convert(varchar,GETDATE(),114) < convert(varchar,ENDTIME,114))
--行

SELECT *
FROM t_time 
WHERE (convert(varchar,'9:00:00',114) > convert(varchar,BEGINTIME,114) AND (convert(varchar,'9:00:00',114) < convert(varchar,ENDTIME,114))
--也行