求两个日期时间段内的所有记录

混世罗汉 2009-02-27 08:28:45
数据库是access, 字段pubtime ,格式是: 年-月-日 时:分:秒格式的。默认是now()。 如:2009-2-27 8:12:22

现在通过asp程序,根据两个日历控件选择的日期段,显示所有的 pubtime在这个时间段的记录,如 2009-2-1 到 2009-2-28 所有发布的新闻。

请各位给个 sql语句。

最好在sql语句中实现,谢谢啊。 sqlserver数据库的会,access的不会,死活搞不出来。
...全文
1050 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiajian19820809 2009-03-08
  • 打赏
  • 举报
回复
ddddddddddddd
lzp4881 2009-03-08
  • 打赏
  • 举报
回复
sql="select * from table where dateValue(pubtime) between '2007-12-01' and '2007-12-30'"
lhy007 2009-03-05
  • 打赏
  • 举报
回复
在前端Ed时间加一天就可以啊
select * from table where pubtime>=sd and pubtime<ed
MikeCheers 2009-03-05
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 mikecheers 的回复:]
表字段time1
-------------
2008-03-02 12:30:20
2008-03-03 12:20:20
2008-03-02 00:00:00
2008-03-04 00:00:00

语句1:SELECT * from tableTime where time1 > #2008-03-02# and time1 < #2008-03-04#
-------------
2008-03-02 12:30:20
2008-03-03 12:20:20

语句2:SELECT * from tableTime where time1 >= #2008-03-02# and time1 <= #2008-03-04#
-------------
2008-03-02 12:30:20
2008-03-…
[/Quote]

看到区别了吧 其实 #2008-03-04#就是2008-03-04 00:00:00
或者可以 SELECT #2008-03-04# 来证实一下
剩下的问题不难解决 再灵活运用一下DateAdd函数 就轻松搞定啦 说多了 说多了...
hncz_zhoumin 2009-03-04
  • 打赏
  • 举报
回复
顶!!!
fjw8205 2009-03-04
  • 打赏
  • 举报
回复
between..and应该很容易解决这个问题吧,不过Access的日期查询需要在日期前后加上“#”符号。
angellan 2009-03-04
  • 打赏
  • 举报
回复

string sql="select * from table where pubtime between '2007-12-01' and '2007-12-30'
MikeCheers 2009-03-04
  • 打赏
  • 举报
回复
表字段time1
-------------
2008-03-02 12:30:20
2008-03-03 12:20:20
2008-03-02 00:00:00
2008-03-04 00:00:00

语句1:SELECT * from tableTime where time1 > #2008-03-02# and time1 < #2008-03-04#
-------------
2008-03-02 12:30:20
2008-03-03 12:20:20

语句2:SELECT * from tableTime where time1 >= #2008-03-02# and time1 <= #2008-03-04#
-------------
2008-03-02 12:30:20
2008-03-03 12:20:20
2008-03-02 00:00:00
2008-03-04 00:00:00

看到区别了吧 其实 #2008-03-04#就是2008-03-04 00:00:00
或者可以 SELECT #2008-03-04# 来证实一下
剩下的问题不难解决
novacheng 2009-03-04
  • 打赏
  • 举报
回复
select * from 表名 where 1=1

and CONVERT(VARCHAR(10),表开始时间字段,120)>= '2009-2-1'

and CONVERT(VARCHAR(10),表输入时间字段,120)<= '2009-2-28 '
Summer_luoluo 2009-03-04
  • 打赏
  • 举报
回复
到底是datediff还是between。。and。。?
小弟对access也不是很了解,同问
sky11007 2009-03-04
  • 打赏
  • 举报
回复
betwen...and
dadapeer110 2009-03-04
  • 打赏
  • 举报
回复
mark
零点不睡觉 2009-03-04
  • 打赏
  • 举报
回复
没这复杂吧
icanplay2008 2009-03-04
  • 打赏
  • 举报
回复
同意 sy_binbin 说的!!!!!

Craftsman 2009-03-04
  • 打赏
  • 举报
回复
sql="select * from table where format(pubtime,""" & "yyyymmdd" & """)>=""" & "20090101" & """ and format(pubtime,""" & "yyyymmdd" & """)<=""" & "20090201" & """
?sql
select * from table where format(pubtime,"yyyymmdd")>="20090101" and format(pubtime,"yyyymmdd")<="20090201"
skyctr 2009-03-03
  • 打赏
  • 举报
回复
帮顶
huguangwu 2009-03-03
  • 打赏
  • 举报
回复
请参考 sql server 的 datediff,函数

ITniao 2009-03-02
  • 打赏
  • 举报
回复
sd开始时间,ed结束时间

strSQL="select * from table where 1=1 "
strSQL=strSQL&" and order_data >= #"&cdate(sd)&"# "
strSQL=strSQL&" and order_data <= #"&DateAdd("d",1,CDate(ed))&"#"
  • 打赏
  • 举报
回复
怎么还没解决啊?
记得说过了啊

sql="select * from table where datediff('d',pubtime,'"&startdate&"')>=0 and datediff('d',pubtime,'"&enddate&"') <=0" 

startdate为开始时间,enddate为结束时间,都是通过传递过来的参数所得
相当于你这里的2009-2-1 和 2009-2-28
lxf88071332 2009-03-02
  • 打赏
  • 举报
回复
select * from table where time between(time1 and time 2)
不过这种方法有时候要注意时间的格式!比如在存储过程中就要转换convert(varchar,@time1,120)
加载更多回复(16)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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