如何查询指定时间段内的数据?

simonfan 2006-07-06 03:55:42
我想查询 每天8点到12点 12点到15点等时间段内的数据
我的数据库里面有一个时间字段是Datetime型的

举例子:2006-07-05 10:20:43
2006-07-05 11:50:41
2006-07-06 15:20:43
2006-07-07 16:00:00
如果 要查询10:00 到16:00 这个时间段数据 这4条都会列出来

...全文
202 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
冷箫轻笛 2006-07-06
  • 打赏
  • 举报
回复
-_-!
simonfan 2006-07-06
  • 打赏
  • 举报
回复
谢谢各位了!
LouisXIV(夜游神)的比较好一些
playwarcraft 2006-07-06
  • 打赏
  • 举报
回复
create table T (date_time datetime)
insert into T
select convert(datetime,'2006-07-05 10:20:43')
union
select convert(datetime,'2006-07-05 11:50:41')
union
select convert(datetime,'2006-07-06 15:20:43')
union
select convert(datetime,'2006-07-07 16:00:00')
union
select convert(datetime,'2006-07-07 16:00:01')

declare @sdate datetime,@edate datetime
set @sdate=convert(datetime,' 10:00:00')
set @edate=convert(datetime,' 16:00:00')
select * from T
where convert(datetime,right(convert(char(20),date_time,120),10))
between @sdate and @edate

drop table T

/*The result:*/
date_time
------------------------------------------------------
2006-07-05 10:20:43.000
2006-07-05 11:50:41.000
2006-07-06 15:20:43.000
2006-07-07 16:00:00.000
LouisXIV 2006-07-06
  • 打赏
  • 举报
回复
^^;

这样写16点整被你刷掉了
冷箫轻笛 2006-07-06
  • 打赏
  • 举报
回复
哦,是哈

那就这样写
select * from tablename
where datepart(hour,columnname) between 10 and 15
LouisXIV 2006-07-06
  • 打赏
  • 举报
回复
--楼上有问题,16点到17点的也会误选出
declare @t table
(
timecolumn datetime
)
insert into @t
select '2006-07-05 10:20:43' union all
select '2006-07-07 16:00:00' union all
select '2006-07-07 16:00:20'

select * from @t
where convert(varchar,timecolumn,108) between '10:00:00' and '16:00:00'

/*
timecolumn
------------------------------------------------------
2006-07-05 10:20:43.000
2006-07-07 16:00:00.000
*/
冷箫轻笛 2006-07-06
  • 打赏
  • 举报
回复
select * from tablename
where datepart(hour,columnname) between 10 and 16
云中客 2006-07-06
  • 打赏
  • 举报
回复
关注

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧