Jump to content

Recommended Posts

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

Is it a DB table? If so, use the correct SELECT.
Help: Distinct, Min, Max

Share this post


Link to post
48 minutes ago, Henry Olive said:

Thank You Stano

No it is not a DBTable, CDS1 table is a Query Result

:classic_biggrin:

So it's a DB table. Just copy and edit the original query. You will have two queries.

Share this post


Link to post
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 by programmerdelphi2k
  • Like 1

Share this post


Link to post
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 by aehimself

Share this post


Link to post
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

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×