TYPE
TStringListHelper = CLASS HELPER FOR TStrings
FUNCTION ToSQL : STRING;
END;
FUNCTION TStringListHelper.ToSQL : STRING;
VAR
S : STRING;
FUNCTION QuotedStr(CONST S : STRING) : STRING;
BEGIN
Result:=''''+ReplaceStr(S,'''','''''')+''''
END;
BEGIN
Result:='';
FOR S IN Self DO BEGIN
IF Result='' THEN Result:='(' ELSE Result:=Result+',';
Result:=Result+QuotedStr(S)
END;
IF Result<>'' THEN Result:=Result+')'
END;
SL:=TStringList.Create;
SL.Add('One');
SL.Add('Two');
SL.Add('Number Three');
SL.Add('It''s number 4');
WRITELN('SELECT * FROM TABLE WHERE FIELD IN '+SL.ToSQL);
SELECT * FROM TABLE WHERE FIELD IN ('One','Two','Number Three','It''s number 4'}
This would be helpful: {USES Classes,StrUtils;
TYPE TStringListHelper = CLASS HELPER FOR TStrings FUNCTION ToSQL : STRING; END;
FUNCTION TStringListHelper.ToSQL : STRING; VAR S : STRING;
FUNCTION QuotedStr(CONST S : STRING) : STRING; BEGIN Result:=''''+ReplaceStr(S,'''','''''')+'''' END;
BEGIN Result:=''; FOR S IN Self DO BEGIN IF Result='' THEN Result:='(' ELSE Result:=Result+','; Result:=Result+QuotedStr(S) END; IF Result<>'' THEN Result:=Result+')' END;
SL:=TStringList.Create; SL.Add('One'); SL.Add('Two'); SL.Add('Number Three'); SL.Add('It''s number 4'); WRITELN('SELECT * FROM TABLE WHERE FIELD IN '+SL.ToSQL);
SELECT * FROM TABLE WHERE FIELD IN ('One','Two','Number Three','It''s number 4'}