I was wondering if you could take a look at my code for date comparison on Delphi.
Code snippet in DELPHI:-
// SQL QUERY to gather member information.
DMS.ADOQuery1.SQL.Clear;
DMS.ADOQuery1.SQL.Add('select HPFROMDT, HPthruDt, MEMBERKEY, MEMBHPKEY, OPFROMDT, OPTHRUDT, HPCODEKEY' +
' from MEMBHP' +
' where MEMBERKEY = ''' + MembKey + ''' and OPFROMDT <= ''' + date_request + ''' and OPTHRUDT > ''' + date_request +''' '
Script snippet in SQL:-
SELECT [MEMBHPKEY]
,[MEMBERKEY]
,[CURRHIST]
,[HPFROMDT]
,[OPFROMDT]
,[OPTHRUDT]
,[HPCODEKEY]
,[HPOPTIONKEY]
FROM [main].[dbo].[*****]
where MEMBERKEY = '1234567' and OPFROMDT <= '2007-08-01' and OPTHRUDT > '2007-08-01'
The SQL script obviously compares a constant date value with the extracted date values. And it works!
However, the Delphi code doesn’t work. The error message
Conversion failed when converting character string to smalldatetime
date type.
I believe there has to be some kind of conversion technique for Delphi that should allow me to convert into the right variable type…. Any ideas?
I extract date_request as a string variable from a text file….
for i := 11 to length(buffer) do
begin
DT_request := DT_request + buffer[i];
end;
The extraction process was picking up a garbage ‘*’ value at the end of the date. This was the problem during date comparison in SQl.
I used
and it showed me that the date_request was actually resulting into a ‘20070815*’… Once I figured that out, it was an easy fix. Remove the ‘*’ and compare just as I did previously.