Solved

How to concatenate strings from fields of different records in a Paradox database

Posted on 2004-08-18
3
870 Views
Last Modified: 2010-04-05
Hi, can anyone help me on this:

I have a paradox database that f.i. contains a field ''Unit" ( string)

What I want to do is to concatenate the value of this field , I'l get more specific with an example

record 1: Unit = "Mario"
record 2: Unit = "Peter"
record 3: Unit = "Marc"

Result --> "Mario / Peter / Marc" --> string

Can this be done by using SQL ?

thx
Marioway
0
Comment
Question by:marioway
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 500 total points
Comment Utility
use a tquery

place a statement like

select unit from yourtablename

adjust the other propertys of the query (ie. databasename->your alias)

then use a buttonclickevent like

var result : String;
begin
  result := '';
  query1.open;
  while not query1.eof do
    result := result * query1.fieldByName('unit').AsString + ' / ';
  //remove last ' / '
  delete(result,length(result)-3,3);
  showmessage(result);
end;

just from head -> typos possible

hope this helps

meikl ;-)
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
Comment Utility
I doubt you can do it with the way your database is set up. You can concatenate strings from different fields in a single record (e.g. SELECT Field1||Field2 FROM SomeTable) but as far as I know, you can't do it on various records.

If you only want it for a few records, there is a bit of a long winded approach to your problem. Assuming your unique record identifier is IDNumber, and you want to concatenate the records for IDNumbers "123", "456" and "789" do the following:
1.) save the following as a text file in your database directory:
     SELECT IDNumber, Unit FROM YourTable WHERE IDNumber = 123 /*or "123" if it is a text field*/
    -> Call it Source1.sql for purposes of this example

2.) save the following as a text file in your database directory:
     SELECT IDNumber, Unit FROM YourTable WHERE IDNumber = 456
    -> Call it Source2.sql for purposes of this example

3.) save the following as a text file in your database directory:
     SELECT Unit FROM YourTable WHERE IDNumber = 789
    -> Call it Source3.sql for purposes of this example

4.) Execute the following query:
    SELECT s1.Unit||"/"||s2.Unit||"/"||s3.Unit
    FROM "c:\YourDBPath\source1.sql" s1,
             "c:\YourDBPath\source2.sql" s2,
             "c:\YourDBPath\source3.sql" s3

E.G. Using the DBDemos table Industry I have done this using the following:
- SELECT * FROM Industry WHERE IND_CODE = 3579 saved as Source1.sql
   in "C:\Program Files\Common Files\Borland Shared\Data\"
- SELECT * FROM Industry WHERE IND_CODE = 3710 saved as Source2.sql
   in "C:\Program Files\Common Files\Borland Shared\Data\"
- SELECT * FROM Industry WHERE IND_CODE = 7000 saved as Source3.sql
   in "C:\Program Files\Common Files\Borland Shared\Data\"

- Execute this query:
  SELECT s1.IND_NAME||"/"||s2.IND_NAME||"/"||s3.IND_NAME AS UnitCombined
  FROM
  "C:\Program Files\Common Files\Borland Shared\Data\Source1.sql" s1,
  "C:\Program Files\Common Files\Borland Shared\Data\Source2.sql" s2,
  "C:\Program Files\Common Files\Borland Shared\Data\Source3.sql" s3
- Results:
  Softw/Auto/Hotel

As I said, this is a rather longwinded approach. Depending on your needs and on how many records you need to concatenate, you might find something like the following more useful:

function ConcatenateFields(DataSet: TDataset; ConcatField: string): string;
begin
  result:= '';
  DataSet.First;
  While not DataSet.Eof do
  begin
    result:= result+DataSet.FieldByName(ConcatField).AsString+'/';
    Dataset.Next;
  end;
  if result <> '' then result:= copy(result,1,Length(result)-1); //get rid of the last slash
end;


Hope this helps.

Kind regards
Pierre Cornelius
0
 

Author Comment

by:marioway
Comment Utility
Hello, Kretchzmar, I changed th code a bit, now it looks like this and it works fine

procedure TForm1.Button1Click(Sender: TObject);
var i: integer ;
    i1, i2, i3 : string;
begin
query1.Close;   (Select Eenheidsmaat from artikel.db)
query1.Prepare;
Query1.Open;
i1:= Query1Eenheidsmaat.Value;   (or Unit in the question)
for i:= 0 to query1.RecordCount - 1 do
 begin
  i3:= i1;
  query1.Next;
  i2:= '/' + query1Eenheidsmaat.Value;
  i1:= i3 + i2;
 end;
Label1.Caption:= i3;

thx alot

Marioway
end;
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now