Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI i have this code its work fine when i search a number inside column
I want the same code when i search a name in column and a date inside different column thinks
<pre lang="Delphi">
procedure TForm1.sEdit1Change(Sender: TObject);
 var a :integer;
begin
IF sEdit1.Text <> '' then
begin
a:=strtoint(sEdit1.Text);
adoquery1.active:=false;
adoquery1.sql.clear;
ADOQuery1.SQL.Add('select * from employé where num_emp='+inttostr(a));
adoquery1.open;
end
else
begin
adoquery1.active:=false;
adoquery1.sql.clear;
ADOQuery1.SQL.Add('select * from employé');
adoquery1.open;
end;
 end;   


What I have tried:

CHANGING the code to string and date
Posted
Updated 22-Aug-18 2:01am
Comments
Herman<T>.Instance 22-Aug-18 7:27am    
T-SQL WHERE Clause has AND option!

1 solution

You just have to know about the SQL syntax. As already mentioned by digimanus you can use AND within WHERE clauses.

But you should use parametrised queries to avoid SQL injection - Wikipedia[^] when the data has been entered by users. Such allow also passing data by value instead of string which is especially useful with dates (no conversion problems) and floating point values (no rounding errors).

Untested example:
Delphi
var
    Param : TParameter;
{ ... }

ADOQuery1.SQL.Add('SELECT * FROM employé WHERE name_column = :Name AND date_column = :Date');
Param := ADOQuery1.Parameters.ParamByName('Name');
Param.DataType := ftString;
Param.Value := varName;
Param := ADOQuery1.Parameters.ParamByName('Date');
Param.DataType := ftDate;
Param.Value := varDate;

Related links:
ADOQuery (Delphi) - RAD Studio Code Examples[^]
TADOQuery.Parameters Property[^]

Read also about the SQL syntax of the used database engine. While the basic syntax is the same there are some differences like for quoting identifier names which must be done when such identifiers are reserved words. You have to do that also if you have a datetime in the database and want to match a date without time. Each database engine has different commands to get the date portion of a datetime for comparison.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900