Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

SQL where only part of string is selected

Hi,

I would like to create a SQL where I only select the unique first character of the field CompanyName.

So the SQL should be adjusted a little bit.
Select Distinct CompanyName from Companies

Result should be
A
G
H  etc.

The second thing I have to do is checking if a caption of a button is within the query-result and if so enable it.

procedure TFBedrijven.FilterknoppenBijwerken;
var i : Integer;
begin
  for i := 0 to ComponentCount-1 do
    if (Components[i] is TdxBarButton) then
     TdxBarButton(Components[i]).Enabled := TdxBarButton(Components[i]).Caption  >>   IN Queryresult(A,G,H);
end;

Thank you for your help.
Stef
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stef Merlijn

ASKER

I'm using an MS Access database.
Found one..
SELECT Distinct MID(Bedrijven.Bedrijfsnaam,1,1) FROM Bedrijven Order by 1;
hmm, guess acces has similar function, not in mind yet

about the array,
you could declare the array also like

Buttons : array[ord('A')..ord('Z')] of TdxBarButton;

disable would the be

for i := ord('A') to ord('Z') do
    Bottons[i].enabled := false;

iteration then

  while not dataset.eof do
    Buttons[ord(dataset.fields[0].asstring[1])].enabled := true;

meikl ;-)
oops, you found one :-))
not seen your comment before
Where do I place the init array?

  Buttons[0] := mbA
  Buttons[1] := mbB
  etc.
One more question:
I also have a button with 0 (zero) which is used for all numeric values.
Ho to add that to the array etc.?
>Where do I place the init array?
best in the oncreate-event of your form,
this must be done only once

>also have a button with 0 (zero)
bad, in this case an array is useless or we have some empty slots

chars 0-9 are ords 48-57
chars A-Z are ords 65-90

seven slots would be unused (58-64) chars :;<=>?@

i think about that

meikl ;-)
This code is looping forever. But we are getting there.
  while not dataset.eof do
    Buttons[ord(dataset.fields[0].asstring[1]) - 64].enabled := true
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So only the part with the 0 (zero) value remains.
The rest is working perfectly. Thanks a lot.

Stef
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes that seems to be the solution if not the 0 (zero) should be set to True when any numeric value is the first character.
So 0,1,2,3,4,5,6,7,8 and 9.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well Meikl, you earned your point with ease.
Thank you.
Stef
glad you got it work :-))

good luck again

meikl ;-)
Just some small adjustment:
The offset must be 64 after all.

  // Deze procedure zet de verschillende filterknoppen aan waarvoor
  // daadwerkelijk gegevens beschikbaar zijn.
  With DM.QAlgemeenGebruik do
  begin
    Close;
    SQL.Clear;
    SQL.Add(' SELECT Distinct MID(Bedrijven.Bedrijfsnaam,1,1) AS BeginLetter FROM Bedrijven Order by 1 ');
    Open;
  end;
  for i := 0 to 26 do
    MyButton[i].enabled := false;
  DM.QAlgemeenGebruik.First;
  while not DM.QAlgemeenGebruik.Eof do
  begin
     if DM.QAlgemeenGebruik.Fields[0].asstring[1] in ['0'..'9'] then  //change here
       MyButton[0].enabled := true
     else
       MyButton[Ord(DM.QAlgemeenGebruik.Fields[0].asstring[1]) - 64].enabled := true; //offset should be 64
     DM.QAlgemeenGebruik.Next;  //missed this
  end;
>The offset must be 64 after all.

oops, my mistake, sorry :-))