我用三个edit来分别输入年、月、日,然后再组合成一个日期形式的值,再用另外三个edit来分别输入年、月、日,也是组合成一个日期形式的值,用这两个值去查某个日期范围(如出生日期的范围),不知道应该怎么做呢?注:后台数据库是SQL Server2000 ,而那字段‘出生日期’的类型只能是datetime类型。还有,最好是能判别出这两个值(即两个日期)的哪一个在前,哪一个在后,这样就不会那么容易出错了。
解决方案 »
- 高分寻高手指点,多进程窗口嵌入问题.
- 最简单的字符串操作,请大家再看看
- 为什么每次运行程序的时候,ADOConnection总提示要输入密码?
- 请问用API怎么得到有图标菜单的字符串呀?
- 辞职,散分
- 请问TStatusPanelStyle = (psText, psOwnerDraw)中psOwnerDraw如何解释,我看了帮助但还是不太明白其含义.大家帮我解释下好吗?
- 多线程计算数据源的问题。。。。。。。
- 新手小问题
- 原来写C++,VB.现在公司要用DELPHI,我是处学者,希望能介绍几本书指导指导!!!有没有免费的DELPHI电子书的下载网站!!!!!谢谢
- 请教一个变量的难题??
- 关于客户端连接时,提示服务器拒绝访问!
- 想做一固定資產管理系統,..........
var
Date1, Date2: TDateTime;
begin
Date1 := EncodeDate(StrToIntDef(Edit1.Text), StrToIntDef(Edit2.Text), StrToIntDef(Edit3.Text));
Date2 := EncodeDate(StrToIntDef(Edit4.Text), StrToIntDef(Edit5.Text), StrToIntDef(Edit6.Text));
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('Select * from TableName Where aDate between :Date1 and :Date2');
Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
Open;
end;
end;
Uses Math;假设Edit1/Edit2/Edit3为第一年月日, Edit4/Edit5/Edit6为第二个年月日procedure TForm1.Button1Click(Sender: TObject);
var
Date1, Date2: TDateTime;
begin
Date1 := EncodeDate(StrToIntDef(Edit1.Text, 2004), StrToIntDef(Edit2.Text, 1), StrToIntDef(Edit3.Text, 1));
Date2 := EncodeDate(StrToIntDef(Edit4.Text, 2004), StrToIntDef(Edit5.Text, 1), StrToIntDef(Edit6.Text, 1));
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('Select * from TableName Where aDate between :Date1 and :Date2');
Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
Open;
end;
end;
Access violation at address 004A7D1D in module 'BOOK.EXE'. Read of address FFFFFFFF'
另外,编译时还会出现一个警告:[Warning] book.pas(300): Variable 'date1' might not have been initialized
请问上面这错误和警告怎样解决啊??
Select_SQL,BigDatetime,smallDatetime:string;
begin
// 获取时间信息
if DateTimePickerStart.DateTime>DateTimePickerEnd.DateTime then
begin
Application.MessageBox('起始日期不能大于结束日期,请重新输入!', '提示', MB_OK + MB_ICONWARNING);
DateTimePickerStart.SetFocus;
abort;
end
else
begin
BigDatetime:=FormatDateTime('yyyymmdd',DateTimePickerStart.DateTime);
smallDatetime:=FormatDateTime('yyyymmdd',DateTimePickerEnd.DateTime);
end;
// 设置查询条件
Select_SQL:='select Gtin as 全球贸易项目代码 , Cn_Name as 产品中文名称 , Editor as 用户名 , InsertTime as 录入时间 From Baseinfo '
+' where InsertTime>='''+ Own_Date_Format(BigDateTime)+' 00:00:00'+''' and InsertTime<='''+Own_Date_Format(SmallDateTime)+' 23:59:59'+'''';
if Trim(Man_edit.Text) <>'' then
Select_SQL:=Select_SQL+ ' and Editor= '''+ trim(Man_edit.Text)+ '''';
if Trim(DM_edit.Text)<>'' then
Select_SQL:=Select_SQL+' and gtin= '''+Trim(DM_edit.Text)+'''';
Select_SQL:=Select_SQL+' Order by Gtin ';
Function Own_Date_Format(datastr:string): string;
begin
if length(datastr)<> 10 then
result:='';
result:=copy(datastr,1,4)+'-'+copy(datastr,5,2)+'-'+copy(datastr,7,2);
end;
环境: SQL Server 2000 + Win2003 + Delphi 6
数据库: NorthWind
测试数据:Edit1..6分别为 1996 01 01 1999 01 01;procedure TForm1.Button1Click(Sender: TObject);
var
Date1, Date2: TDateTime;
begin
Date1 := EncodeDate(StrToIntDef(Edit1.Text, 2004), StrToIntDef(Edit2.Text, 1), StrToIntDef(Edit3.Text, 1));
Date2 := EncodeDate(StrToIntDef(Edit4.Text, 2004), StrToIntDef(Edit5.Text, 1), StrToIntDef(Edit6.Text, 1));
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('Select * from Orders Where OrderDate between :Date1 and :Date2');
Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
Open;
end;
end;
var
Date1, Date2: TDateTime;
begin
Date1 := EncodeDate(StrToInt(Edit1.Text), StrToInt(Edit2.Text), StrToInt(Edit3.Text));
Date2 := EncodeDate(StrToInt(Edit4.Text), StrToInt(Edit5.Text), StrToInt(Edit6.Text));
with ADOquery1 do
begin
Close;
SQL.Clear;
SQL.Add('Select * from tablename Where adate between :Date1 and :Date2');
Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
Open;
end;
end;end.
var
Date1, Date2: TDateTime;
begin
Date1 := EncodeDate(StrToIntDef(Edit1.Text), StrToIntDef(Edit2.Text), StrToIntDef(Edit3.Text));
Date2 := EncodeDate(StrToIntDef(Edit4.Text), StrToIntDef(Edit5.Text), StrToIntDef(Edit6.Text));
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('Select * from TableName Where aDate between :Date1 and :Date2');
Parameters.ParamByName('Date1').Value := Min(Date1, Date2);
Parameters.ParamByName('Date2').Value := Max(Date1, Date2);
Open;
end;
end;
begin
with DataM.InpBillADOQ do
begin
close;
sql.Clear;
sql.Add('select a.item_name,a.item_spec,a.amount,a.units,a.costs/a.amount,a.costs, ');
sql.Add('b.drug_insur_type1||b.pay_pay1,a.billing_date_time ');
sql.Add('from inp_bill_detail a,price_vs_insur b ');
sql.Add('where a.item_code=b.item_code ');
sql.Add('and a.patient_id='''+trim(IdEdit.Text)+''' ');
sql.Add('and trunc(a.billing_date_time) >=:std1 ');
sql.add('and trunc(a.billing_date_time) <=:std2 ');
sql.Add('order by a.billing_date_time ');
Parameters.ParamValues['std1']:=strToDateTime(DateToStr(BeginDateTimeP.Date)+' 00:00:00');
Parameters.ParamValues['std2']:=StrToDateTime(DateToStr(EndDateTimeP.Date)+' 23:59:59');
prepared;
open;
//////////////////////////////////////////////////////////////
DataM.SumADOQ.Close;
DataM.SumADOQ.SQL.Clear;
std1 := datetostr(BeginDateTimeP.Date)+' 00:00:00' ;
std2 := datetostr(EndDateTimeP.Date)+' 23:59:59';
DataM.SumADOQ.SQL.Add('select sum(costs) from inp_bill_detail ');
DataM.SumADOQ.sql.Add('where patient_id='+''''+trim(IdEdit.Text)+'''');
DataM.SumADOQ.sql.Add(' and billing_date_time>='+ 'to_date('+''''+std1+''''+','+''''+'yyyy-mm-dd hh24:mi:ss'+''''+')');
DataM.SumADOQ.sql.add(' and billing_date_time<='+ 'to_date('+''''+std2+''''+','+''''+'yyyy-mm-dd hh24:mi:ss'+''''+')');
DataM.SumADOQ.open;
Label10.Caption:=DataM.SumADOQ.Fields[0].AsString;
DataM.SumADOQ.Close;
//////////////////////////////////////////////////////////////
if recordcount<1 then
application.MessageBox('该病人在此时间段内没有发生费用!','系统提示',32);
exit;
close;
end;
end;