Monday, February 2, 2009

To split a string in Crystal report formulae editor

          This is a code to split a long string in the database to be used in crystal reports, for eg. an address field with each particular sectors in address separated by a delimiter like a comma.First of all you have to create a new formula field like @address and edit the formula with the formula editor which is provided inside the crystal reports.Below is the input database string to be split
Address field stored in the database:
House No-999,Sector-343,Mayhelm Street,PostOffice,657466
The code to be written inside the formulae editor for @address formulae field to display the address after splitting is given below
stringVar array x := split({Tablename.Addressfield},",");
Local numberVar i;
Local stringVar outputString := "";
For i:=1 to Count(x) do
(
outputString := outputString + x[i] + Chr(10)
);
outputString;
The outputString is the result which is the wanted address in the format given below
House No-999,
Sector-343,
Mayhelm Street,
PostOffice,
657466
Explanation of the code:
          The address field from the database is first of all split using the function Split(field,delimiter) in the formula editor.This is stored in an array and then iterated and concatenated one by one to the outputString.The Chr(10) used is to jump to new line after printing each strings seperated after comma.