Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Concatenate fields in SQL ?
Message
 
 
À
22/03/2002 13:16:10
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00636165
Message ID:
00636181
Vues:
18
>Is there any way to concatenate character fields from several records in a SQL statement ?
>
>
>Emp    Date        Note
>---------------------------
>1     {1/1/1}      Project 1
>1     {1/2/1}      Assist Kim
>2     {1/1/1}      Sick
>2     {1/2/1}      Project 3
>
>results desired:
>
>Emp   Notes
>---------------------------
>1     Project 1 chr(13) Assist Kim chr(13)
>2     Sick chr(13) Project 3 chr(13)
>
>
>TIA

You cannot do that in the query w/o using UDF(). The lenght of of Notes field in this case is limited to 254 characters.
Select Emp, fnNotes(Emp) As Notes ;
  FROM mytable ;
  GROUP BY Emp
...
FUNCTION fnNote
LPARAMETERS tnEmp
LOCAL laArray[1], i, lcNotes
Select Note ;
  FROM mytable ;
  WHERE Emp = tnEmp ;
  INTO ARRAY laArray

lcNotes = ""
FOR i=1 TO LEN(laArray)
 lcNotes = lcNotes + laArray[i] + CHR(13)
ENDFOR
RETURN lcNotes
Remember that query with UDF could be quite slow on the big tables.
You can find an alternative solution in the Message #539349.
One more way
CREATE CURSOR crsNotes (Notes M)
Select Emp, crsNotes.Notes ;
  FROM mytable ;
  GROUP BY Emp ;
  INTO CURSOR crsResult ReadWrite
SCAN
  lnEmp = Emp
  Select Note ;
    FROM mytable ;
    WHERE Emp = lnEmp ;
    INTO ARRAY laArray

  lcNotes = ""
  FOR i=1 TO LEN(laArray)
    lcNotes = lcNotes + laArray[i] + CHR(13)
  ENDFOR
  REPLACE Notes WITH lcNotes 
ENDSCAN
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform