RobertKing
RobertKing

Reputation: 1911

SSRS expression to get first character from string

I am having a field with string values as "First Middle Last" and i want to show the initial characters from this string as "FML"

how can i do it in terms of ssrs expression ?

Upvotes: 1

Views: 12983

Answers (1)

Jonnus
Jonnus

Reputation: 3028

Assuming the field MyString always has 3 words the following will find the first character of the First, Second and Last words. This admittedly doesn't handle instances where there are more or less than 3 words, but hopefully should get you started if you require more finesse.

=Left(Fields!MyString.Value, 1) + " " +
 Left(Mid(Fields!MyString.Value, InStr(Fields!MyString.Value, " ") + 1), 1) + " " +
 Left(Mid(Fields!MyString.Value, InStrRev(Fields!MyString.Value, " ") + 1), 1)

Edit

To cope with the possiblity of only two words (as suggested in the commetns below) a check for the index of the spaces could be used to ensure that they are not the same, and thus 3 words exist. This would make the code as follows

=Left(Fields!MyString.Value, 1) + " " +
 Left(Mid(Fields!MyString.Value, InStr(Fields!MyString.Value, " ") + 1), 1) +
 iif(InStrRev(Fields!MyString.Value, " ") > InStr(Fields!MyString.Value, " "),
   " " + Left(Mid(Fields!MyString.Value, InStrRev(Fields!MyString.Value, " ") + 1), 1),
   "")

Upvotes: 1

Related Questions