32snl
32snl

Reputation: 13

Crystal - Split string based on carriage return

I have a field like this, where I only want the first/latest entry to show on the Crystal report:

05/01/2018 00:00:00 pm notes

04/01/2018 00:00:00 pm more notes

03/01/2018 00:00:00 pm even more notes

Here's the code I have been trying to work with, but I get an error of "a subscript must be between 1 and the size of the array." Can someone please help point me in the right direction?

stringvar array csl;
stringvar return;
csl:=split({table.field},chr(13));
//csl[1]
if isnull({table.field}) then return:= ""
else return:=csl[1];
return;

Upvotes: 1

Views: 1984

Answers (1)

MatSnow
MatSnow

Reputation: 7517

The reason for the error is presumably because the check for null happens after the split.

I usually try to avoid using arrays/variables in Crystal Reports. Mainly because you can't group or use aggregate functions on formulas that contain variables.

So here's a solution that works with string-functions:

First Entry

If InStr({table.field}, chr(13)) > 0 Then
    Left({table.field}, InStr({table.field}, chr(13)))
Else
    {table.field}

Last Entry

If InStrRev({table.field}, chr(13)) > 0 Then
    Right({table.field}, Len({table.field}) - InStrRev({table.field}, chr(13)))
Else
    {table.field}

EDIT

To get the second line too you have to do the InStr/InStrRev two times:

First Entry

If InStr(InStr({@table.field}, chr(13))+1,{@table.field}, chr(13)) > 0 Then
    Left({@table.field}, InStr(InStr({@table.field}, chr(13))+1, {@table.field}, chr(13)))
Else
    {@table.field}

Last Entry

If InStrRev({@table.field}, chr(13), InStrRev({@table.field}, chr(13))+1) > 0 Then
    Right({@table.field}, Len({@table.field}) - InStrRev({@table.field}, chr(13),InStrRev({@table.field}, chr(13))-1))
Else
    {@table.field}

Upvotes: 1

Related Questions