首页 新闻 会员 周边

数据库是日期型的问题

0
悬赏园豆:20 [已解决问题] 解决于 2009-07-08 11:22

大家好,希望看到的朋友能帮帮忙!这个问题困扰了我三天了,相关的文章我都找不到。在此先谢谢各位了。问题是这样的:在我的access数据库中有一个日期型的字段,我存储的时间,但是我想获取晚上18:00到明天早上9:00的数据,我却怎么也获得了不过来。谢谢各位!

Wennie的主页 Wennie | 初学一级 | 园豆:55
提问于:2009-07-07 11:00
< >
分享
最佳答案
0
SELECT ID,PName,PDate from testtable
where cdate(PDate)>=cdate('2009-7-6 16:00')
and cdate(PDate)<=cdate('2009-7-7 9:00')

---或

SELECT ID,PName,PDate from testtable
where cdate(PDate)>=#2009-7-6 16:00#
and cdate(PDate)<=#2009-7-7 9:00#


--今天09:00至 昨天16:00后

SELECT ID,PName,PDate from testtable
where
(cdate(PDate)
>=dateadd('h',-8,date()))
and (cdate(PDate)<=dateadd('h',9,date()))
--
SELECT ID,PName,PDate from testtable
where (cdate(PDate) between dateadd('h',-8,date()) and dateadd('h',9,date()))

 

 

string str1 = this.txtStartDate.Text.ToString();
string str2 = this.txtEndDate.Text.ToString();
int str3 = Convert.ToInt32(this.txtStartTimes.Text.ToString());
int str4 = Convert.ToInt32(this.txtEndTimes.Text.ToString());
string sql=string.format("select * from WorkEffict where (Dates>#{0} {2}:00# and Dates<#{1} {3}:00#) and (hour(Dates)>={2} or hour(Dates)<={3})",str1,str2,str3,str4)
//原理:查询出开始日期当天16:00以后及截止日期后一天9:00之前且时间数大于16或者小于9的所有记录


后面这段是手写的,你检查一下。
精确到秒, 可以参考

string str1 = this.txtStartDate.Text.ToString();
string str2 = this.txtEndDate.Text.ToString();
string str3 = this.txtStartTimes.Text.ToString();//eg,"16:32:23"
string str4 = this.txtEndTimes.Text.ToString(); // eg,"09:00:00"
string sql = string.format("select * from WorkEffict where (Dates>#{0} {2}# and Dates<#{1} {3}#) and (format(Dates, \"hh:mm:ss\") >=#{2}# or format(Dates, \"hh:mm:ss\") <=#{3}#)", str1, str2, str3, str4);

邀月 | 高人七级 |园豆:25475 | 2009-07-07 15:43
其他回答(3)
0

为什么?你怎么写的sql语句

邢少 | 园豆:10926 (专家六级) | 2009-07-07 11:50
0

Access中不能用btween and 

可以用:

Select * from 表名 where  日期>= #晚上18:00# and 日期 <= #早上9:00#

無獨有偶 | 园豆:43 (初学一级) | 2009-07-07 15:45
0

建议先用字符串处理将”2009-09-10  18:30:00”去掉日期部分where条件用小时比较,写之前考虑一下便捷   18:00:00————24:00:00,0:00:00————9:00:00

牛牛博客 | 园豆:305 (菜鸟二级) | 2009-07-07 16:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册