还是日期时间查询的问题,这次终于表达清楚了!

ffww 2009-04-20 10:35:51
例如表A:,其中有字段DATATMP,TIMETMP,等等

//---------------------------------------------
DATATMP TIMETMP 其它字段
------------------------------------------------
2009-1-1 10:00:00 xxxx
2009-3-1 10:05:00 xxxx
2007-3-2 7:11:00 xxxx
2009-3-2 11:21:00 xxxx
2007-3-2 8:11:00 xxxx
2007-3-3 9:52:00 xxxx
2007-3-3 13:00:00 xxxx
------------------------------------------------
我想获取2009-3-2 号至 2009-3-3的记录,可以这么写:
select *form A where DATATMP>='2009-3-2' and DATATMP<='2009-3-3'

但是,如果我想获取某一个日期/时间段的记录该怎么做?
例:我想获取2009-3-2 号8:11:00 到 2007-3-3 号 13:00:00 的记录?
...全文
124 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
long_0662 2009-04-21
  • 打赏
  • 举报
回复
select * from a
where (datetmp between '2009-3-2' and '2007-3-3')
and (timetmp between '8:11:00' and '13:00:00')
long_0662 2009-04-21
  • 打赏
  • 举报
回复
看过了
ACMAIN_CHM 2009-04-21
  • 打赏
  • 举报
回复

ACCESS 中日期常量以 ##来界定

这样你可以简单地如下。
select * from A where DATATMP>=#2009-3-2 08:11:00# and DATATMP <=#2009-3-3 13:00:00#

当然某些情况下你也可以用CDATE()函数将 '2009-3-2 08:11:00' 转换成日期。

QQ群 48866293 / 12035577 / 7440532 / 13666209
ACCESS专业论坛
http://www.accessbbs.cn/bbs/index.php .
http://www.office-cn.net/vvb/ .
http://www.accessoft.com/bbs/index.asp .
http://www.access-programmers.co.uk/forums .
.
http://www.office-cn.net/home/space.php?uid=141646 .

htl258_Tony 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ffww 的回复:]
请问楼上各位牛人:
如果用在access数据库这样写能行吗?
[/Quote]
access用#作为日期分格符,语法不熟,建议还是到ACCESS版块问下比较快。
taoistong 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ws_hgo 的回复:]
SQL codecreate table #tb1([DATATMP] varchar(10),[TIMETMP] varchar(10),[其它字段] varchar(10))
insert #tb1 select '2009-1-1','10:00:00','xxxx'
union all select '2009-3-1','10:05:00','xxxx'
union all select '2007-3-2','7:11:00','xxxx'
union all select '2009-3-2','11:21:00','xxxx'
union all select '2007-3-2','8:11:00','xxxx'
union all select '2007-3-3','9:52:00','xxxx'
union all select '2007…
[/Quote]

怎一个快字了得
ffww 2009-04-20
  • 打赏
  • 举报
回复
请问楼上各位牛人:
如果用在access数据库这样写能行吗?
ws_hgo 2009-04-20
  • 打赏
  • 举报
回复
create table #tb1([DATATMP] varchar(10),[TIMETMP] varchar(10),[其它字段] varchar(10))
insert #tb1 select '2009-1-1','10:00:00','xxxx'
union all select '2009-3-1','10:05:00','xxxx'
union all select '2007-3-2','7:11:00','xxxx'
union all select '2009-3-2','11:21:00','xxxx'
union all select '2007-3-2','8:11:00','xxxx'
union all select '2007-3-3','9:52:00','xxxx'
union all select '2007-3-3','13:00:00','xxxx'


select * from #tb1 where cast(DATATMP as datetime)+cast([TIMETMP] as datetime)
between '2007-3-3 13:00:00' and '2009-03-02 8:11:00'

DATATMP TIMETMP 其它字段
---------- ---------- ----------
2009-1-1 10:00:00 xxxx
2009-3-1 10:05:00 xxxx
2007-3-3 13:00:00 xxxx

(3 行受影响)
ffww 2009-04-20
  • 打赏
  • 举报
回复
to:htl258

select *
from tb
where cast(DATATMP as datetime)+cast([TIMETMP] as datetime)
between '2007-3-3 13:00:00' and '2009-03-02 8:11:00'

可是结果集中怎么不显示这两条记录呢,我想叫他们也显示。
2007-3-3 13:00:00
2009-03-02 8:11:00
-狙击手- 2009-04-20
  • 打赏
  • 举报
回复
。。
htl258_Tony 2009-04-20
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb] 
go
create table [tb]([DATATMP] varchar(10),[TIMETMP] varchar(10),[其它字段] varchar(10))
insert [tb] select '2009-1-1','10:00:00','xxxx'
union all select '2009-3-1','10:05:00','xxxx'
union all select '2007-3-2','7:11:00','xxxx'
union all select '2009-3-2','11:21:00','xxxx'
union all select '2007-3-2','8:11:00','xxxx'
union all select '2007-3-3','9:52:00','xxxx'
union all select '2007-3-3','13:00:00','xxxx'
go

select *
from tb
where cast(DATATMP as datetime)+cast([TIMETMP] as datetime)
between '2007-3-3 13:00:00' and '2009-03-02 8:11:00'
/*
DATATMP TIMETMP 其它字段
---------- ---------- ----------
2009-1-1 10:00:00 xxxx
2009-3-1 10:05:00 xxxx
2007-3-3 13:00:00 xxxx

(3 行受影响)
*/
  • 打赏
  • 举报
回复
select *
from tb
where cast(datatmp+' '+timetmp as datetime) between '2009-3-2 8:11:00' and '2007-3-3 13:00:00'

34,837

社区成员

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

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