Henry Olive 5 Posted January 14, 2023 Good Day, MyTable looks like below DOCNO 10-01 10-01 20-02 20-02 At the end I want to get ('10-01,20-02') // Every SINGLE DocNo with below code i'm getting '10-01,20-02,20-02' ( 20-02 should have been SINGLE ) DocNos :='' CDS1.First; while not CDS1.Eof do begin if DocNos ='' then DocNos := CDS1DOCNO.asString else if Pos(DocNos, CDS1DOCNO.AsString) = 0 then DocNos := DocNos + ','+ CDS1DOCNO.AsString; CDS1.Next; end; What am i doing wrong ? Thank You Share this post Link to post
Stano 143 Posted January 14, 2023 Is it a DB table? If so, use the correct SELECT. Help: Distinct, Min, Max Share this post Link to post
Henry Olive 5 Posted January 14, 2023 (edited) Thank You Stano No it is not a DBTable, CDS1 table is a Query Result Edited January 14, 2023 by Henry Olive Share this post Link to post
Stano 143 Posted January 14, 2023 48 minutes ago, Henry Olive said: Thank You Stano No it is not a DBTable, CDS1 table is a Query Result So it's a DB table. Just copy and edit the original query. You will have two queries. Share this post Link to post
Henry Olive 5 Posted January 14, 2023 Thank You so much Stano I just found out Contains (StrUtils) I solved my problem with Contains instead of Pos Share this post Link to post
programmerdelphi2k 237 Posted January 14, 2023 (edited) procedure TForm1.Button1Click(Sender: TObject); var LText: string; begin LText := 'old value'; // ... while if not LText.Contains('new value') then LText := LText + ',' + 'new value'; // ... end while ShowMessage( LText.Remove(0,1) ); // remove first comma end; or just Quote select DISTINCT DocNo from TableX; // using any "Query" component/class over your CDs ( see property CommandText usage ) Edited January 14, 2023 by programmerdelphi2k 1 Share this post Link to post
aehimself 396 Posted January 14, 2023 (edited) 6 hours ago, Henry Olive said: I just found out Contains (StrUtils) Be careful with Contains, as it will trigger on substrings too. Maybe it's not the case with DocNos, but still. What I'd do is... DocNos := #9; CDS1.First; While Not CDS1.Eof Do Begin If Not DocNos.Contains(#9 + CDS1DOCNO.asString + #9) Then DocNos := DocNos + CDS1DOCNO.asString + #9; CDS1.Next; End; DocNos := DocNos.Substring(1, DocNos.Length - 2); This also can fail if the strings contain tabulator characters, but that's a small chance I'd be willing to take. The best solution would be to use a TList<String> and concatenate everything once it's filled. Edited January 14, 2023 by aehimself Share this post Link to post
David Schwartz 426 Posted January 15, 2023 On 1/14/2023 at 2:29 AM, Henry Olive said: if Pos(DocNos, CDS1DOCNO.AsString) = 0 then Your test is backwards. It should be: if Pos(CDS1DOCNO.AsString, DocNos) = 0) then ... Share this post Link to post
Henry Olive 5 Posted January 16, 2023 Thank you so much David, actually my code is like yours but when i write here i made a mistake, otherwide Delphi doesnt compile Share this post Link to post
Ian Branch 127 Posted January 16, 2023 23 hours ago, David Schwartz said: if Pos(CDS1DOCNO.AsString, DocNos) = 0) then ... Seems to have a redundant ')' after the =0.. Share this post Link to post
David Schwartz 426 Posted January 17, 2023 5 hours ago, Ian Branch said: Seems to have a redundant ')' after the =0.. Yup, it's a typo. Sorry, I can't edit it. I either added an extra one on the right, or forgot one on the left. Take your pick. 🙂 Regardless, the point was that the parameters to the Pos function are backwards. Share this post Link to post