简体   繁体   中英

Date of type of String in Delphi

We have a field in Access called 'DATE', in which the date is recorded, but is of the type Text. Now, if we want to do a search, how is it possible with 'Between' statement ? I use this code but it does not answer

  ADOQuery1.SQL.Text:= 'Select * From tst Where 1=1';
  ADOQuery1.SQL.Add(' And Date1 Between ' +
  Date1.Format('yyyy/mm/dd',
  Date1.Date) + ' And ' + Date2.Format('yyyy/mm/dd', Date2.Date));

I think I need to convert string to date or number, but I do not know how

As you say the field is text so values used in any where critera are expected to be text - ie strings. Your encoded date values are not represented in the resulting SQL as a string, but as date literals, eg:

select * From tst Where 1=1
and Date1 Between 2018/01/01 and 2018/01/31

You need to use strings as the limits on your query (and also bear in mind that dates are quote delimited in SQL anyway, even when expressed as literals):

select * From tst Where 1=1
and Date1 between '2018/01/01' and '2018/01/31'

Assuming that the representation of the dates in your field are reliably consistent, this should work, thanks to the ymd ordering of your date components in that representation.

A small modification to your SQL construction code to include quotes around the encoded date values should do the trick:

ADOQuery1.SQL.Text:= 'Select * From tst Where 1=1';
ADOQuery1.SQL.Add(' And Date1 Between ''' +
  Date1.Format('yyyy/mm/dd', Date1.Date) + ''' And ''' +
  Date2.Format('yyyy/mm/dd', Date2.Date) + '''');

This alternative approach to building the SQL might make it a little clearer exactly what is going on:

// Obtain string representations of Date values
sDateFrom := Date1.Format('yyyy/mm/dd', Date1.Date);
sDateTo   := Date2.Format('yyyy/mm/dd', Date2.Date);

// Build SQL using string representations of dates in criteria
ADOQuery1.SQL.Text:= 'Select * From tst Where 1=1';
ADOQuery1.SQL.Add(Format(' and Date1 between ''%s'' and ''%s''', [sFromDate, sToDate]));

If you are in a position to change the implementation details of the database in your application, you should seriously consider using an appropriate date or date/time type for such columns which could simplify things when dealing with those columns and needing to work with them as dates or date/times, not least in then being able to use date/time related functions of your SQL engine directly on such columns, for example.

Also worth mentioning is that building SQL using string concatenation can make your code vulnerable to SQL Injection attacks. The specifics of this case mean that the code here is not vulnerable since the values involved are constrained to formatted date values, but this is a consideration and generally speaking parameterized queries are safer and can be significantly more efficient, especially where a query is re-used (executed several times) with only variations in the parameter values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM