紧急求助!(300分)

G

gylgw

Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是access数据库,里面有日期字段,格式为yyyy-mm-dd。我现在想实现在输入两个
日期后,程序能自动显示出这两个日期间的记录,比如说,近期谁生日,我输入2002-9-1
和2002-9-25,则这期间的生日的都能找出来。我的日期字段中包括年-月-日。
最好能给出原代码!!!!!
 
procedure TzwxxForm.Button2Click(Sender: TObject);
var
date1,date2:String;
begin
date1:=Edit1.text;
date2:=Edit2.text;
tmpQuery.Close;
tmpQuery.SQL.Clear;
tmpQuery.SQL.Add('Select * From YourTable ');
tmpQuery.SQL.Add('Where ');
tmpQuery.SQL.Add('RQ>=#'+Date1+'# and RQ<=#'+Date2+'#');
tmpQuery.Open;
.....
end;
 
AdoDataset1.Close;
AdoDataset1.CommandText:='select * from table where birthday>=#2002-9-1# and birthday<=#2002-9-25#';
AdoDataset1.Open;
 
table1表中a3表示时间字段。
procedure TForm1.Button1Click(Sender: TObject);
begin
query1.Close;
query1.SQL.Clear;
query1.SQL.Add('select * from table1 where a3 between :d1 and :d2');
query1.ParamByName('d1').AsDateTime := DateTimePicker1.Date;
query1.ParamByName('d2').AsDateTime := DateTimePicker2.Date;
query1.Prepare;
query1.Open;
end;
 
select * from tablename where year(dt)*1000+month(dt)*100+day(dt) between 20020901 and 20020925
 
我的意思是要找出日期字段中的月-日的部分介于我输入的日期中的月-日之间,
比如说要找出这个月生日的人来。而数据库中的日期字段是包括年-月-日的。
每人的出生日期也不一样啊。但是他们的生日有可能是在同一个月的,或者说相差不多。
其实就是要找出某些人的生日了。
你们说的都是通过yyyy-mm-dd来找。
 
改数据库结构,让year,month,day分开
 
只要你将输入日期中的月和日都分解出来,就可以按我的这一句SQL来查询了:
select * from testA where Month(f_Date)>=9 and Day(F_date)>=1 and Month(F_date)<=9 and Day(F_Date)<=30
上一句查询的是生日在9月1号到9月30日之间的人。分别用month与day函数取日期中的月份与日期数
 
可以将日期时间类型Convert成字符串型再用Substring()分别取其中的月和日转换成整型再分别和输入的进行比较
把这些加到条件中.可能稍微麻烦点
 
Select *
From table1
Where month(rq)*100+day(rq)>=0901 and
month(rq)*100+day(rq)<=0925
 
str:='Select * From YourTable where month(datefild)>'+inttostr(monthof(date1))
+' and month(datefild)<'+inttostr(monthof(date2))+' union 'Select * From YourTable where month(datefild)='
+inttostr(monthof(date1))+'and day(datefild)>='+inttostr(dayof(date1))
+' union 'Select * From YourTable where month(datefild)='
+inttostr(monthof(date2))+'and day(datefild)<='+inttostr(dayof(date2))
tmpQuery.Close;
tmpQuery.SQL.Clear;
tmpQuery.SQL.Add(str);
tmpQuery.Open;
 
SELECT ttt.sn, ttt.dasdfs
FROM ttt
WHERE (((Day([ttt].[dasdfs]))=Day('2002-01-01') And (Month ([ttt].[dasdfs]))=Month('2002-01-01')));
关鍵是day和month函数
 
上一个回答不认真,不能解决跨月的问题,但现在找着了办法:
select * from testselect * from testA where Month(f_Date)*31+Day(F_Date)>=238
and Month(F_date)*31+Day(F_Date)<=304
这是找7月21日到9月25日之间的。
又看了一遍大家的回答,发现QuickSilver的方案比我的更好。不过我们都没有解决跨年
的问题,因此如有跨年,还要如下写(依QuickSilver方法):
select * from testselect * from testA where (Month(f_Date)*100+Day(F_Date)>=1221
and Month(F_date)*100+Day(F_Date)<=1231) or (Month(F_Date)*100+Day(F_Date)<=101
and Month(F_date)*100+Day(F_Date)<=115)
查的是12月21日到1月15日之间的数据。
 
在我准备贴出来之前 HunterTeam 就贴上去了, 如果得了分,你可要分我一份啦.^_^
但可能还有一中情况要考虑,即,结束日期与起始日期跨年,但结束日期的月份大于
起始日期的月份.这样要按一周年计算.
select * from testselect * from testA where (Month(f_Date)*100+Day(F_Date)>=101
and (Month(f_Date)*100+Day(F_Date) <= 1231;
或将HunterTeam大虾的第二中情况改一下:
select DISTINCT field1,field2.. from testselect * from testA where (Month(f_Date)*100+Day(F_Date)>=1221
and Month(F_date)*100+Day(F_Date)<=1231) or (Month(F_Date)*100+Day(F_Date)<=101
and Month(F_date)*100+Day(F_Date)<=115)
 
接受答案了。
 
顶部