日期:2014-05-19  浏览次数:20479 次

关于日期的查询语句
关于日期的查询语句
我的数据库如下:
序号       姓名       性别           开始时间varchar(50)         结束时间varchar(50)
id           xm           xb               kstime                                   jstime
1             张三       男               8:10:00                                 14:22:00
2             李四       男               10:11:00                               12:10:00
3             红红       女               9:10:00                                 10:10:00
4             兵兵       男               12:10:00                               16:00:00

假设当前时间为9:22:23
请问如何找出在开始时间与结束时间内符合当前时间的记录
即找出       id为1、3的记录

------解决方案--------------------
select * from 表 where
datediff(ss,kstime, '9:22:23 ')> =0 and datediff(ss,jstime, '9:22:23 ') <=0
------解决方案--------------------
create table t(id int identity,xm varchar(10),xb varchar(6),kstime varchar(20),jstime varchar(20))
insert into t select '张三 ', '男 ' , '8:10:00 ', '14:22:00 '
insert into t select '李四 ', '男 ' , '10:11:00 ', '12:10:00 '
insert into t select '红红 ', '女 ' , '9:10:00 ' , '10:10:00 '
insert into t select '兵兵 ' , '男 ', '12:10:00 ' , '16:00:00 '

select * from t
where cast(kstime as datetime) < '9:22:23 ' and cast(jstime as datetime) > '9:22:23 '

id xm xb kstime jstime
----------- ---------- ------ -------------------- --------------------
1 张三 男 8:10:00 14:22:00
3 红红 女 9:10:00 10:10:00

(所影响的行数为 2 行)


------解决方案--------------------
select * from t
where datediff(ss,cast(kstime as datetime),convert(varchar(10),getdate(),108))> 0 and datediff(ss,cast(jstime as datetime) ,convert(varchar(10),getdate(),108)) <0

select * from t
where cast(kstime as datetime) <convert(varchar(10),getdate(),108) and cast(jstime as datetime)> convert(varchar(10),getdate(),108)