查询数据语句请帮忙

JASONJJYY 2010-10-11 03:43:31
我有个表TABLE,里面有一个字段时间 查出他一个月里面每天9点到18点的数量 排除9点前和18后的数据,怎么写呢?小弟不才,请多多帮忙。

附加多一个问题吧 如果还需要排除星期六星期日怎么写呢?谢谢帮忙。
...全文
131 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
JASONJJYY 2010-10-11
  • 打赏
  • 举报
回复
个个大哥都系好人 5分 5分 分了吧 谢谢
闹铃 2010-10-11
  • 打赏
  • 举报
回复

select *
from dbo.tb
where datepart(hour,dt) between 9 and 18
and datepart(dw,dt+@@datefirst-1)<>6
and datepart(dw,dt+@@datefirst-1)<>7;
--无论datefirst为几,都会逻辑的认为 周一为一周的第一天(datepart(dw,dt+@@datefirst-n) n可以随便设)
fpzgm 2010-10-11
  • 打赏
  • 举报
回复
注意应该是2-6不是1-5!

select *
from tb
where datepart(hh,时间) between 9 and 18
and datepart(weekday,时间) between 2 and 6
JASONJJYY 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 billpu 的回复:]
SQL code
select * from tb where datepart(hh,time) between 9 and 18 and datepart(dw,time)<6
[/Quote]

请问我的2010-09-01 到 2010-0930写在那里呢
banxcx 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 josy 的回复:]
SQL code
set datefirst 1

select *
from tb
where datepart(hh,时间) between 9 and 18
and datepart(weekday,时间) between 1 and 5
[/Quote]

可以顶一下不
kevn 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 josy 的回复:]
SQL code
set datefirst 1

select *
from tb
where datepart(hh,时间) between 9 and 18
and datepart(weekday,时间) between 1 and 5
[/Quote]
billpu 2010-10-11
  • 打赏
  • 举报
回复
恩 星期天没考虑进去 还是用between吧2 and 6 吧
Oraclers 2010-10-11
  • 打赏
  • 举报
回复
select *
from Table1
where convert(char(8),日期时间,108) between '09:00:00' and '18:00:00' and datepart(dw,日期时间) between 2 and 6
billpu 2010-10-11
  • 打赏
  • 举报
回复
select * from tb where datepart(hh,time) between 9 and 18 and datepart(dw,time)<6
SQLCenter 2010-10-11
  • 打赏
  • 举报
回复
select * from tb
where datepart(hh,时间) between 9 and 18
and (@@datefirst+datepart(weekday,时间)-1)%7 between 1 and 5
百年树人 2010-10-11
  • 打赏
  • 举报
回复
set datefirst 1

select *
from tb
where datepart(hh,时间) between 9 and 18
and datepart(weekday,时间) between 1 and 5
JASONJJYY 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zsh0809 的回复:]
1,时间用between...AND...就可以处理了
2,排除星期六星期日,可以查看一下datename函数的应用。
[/Quote]

不好意思 我写的是1个月的数据 不是1天
JASONJJYY 2010-10-11
  • 打赏
  • 举报
回复
楼上between...AND...我还是查询到18点以外的数据啊
「已注销」 2010-10-11
  • 打赏
  • 举报
回复
1,时间用between...AND...就可以处理了
2,排除星期六星期日,可以查看一下datename函数的应用。

34,591

社区成员

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

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