?
Solved

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

Posted on 2004-08-18
3
Medium Priority
?
1,010 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1500 total points
ID: 11835349
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
ID: 11835635
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
ID: 11839559
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 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