Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Get the first and last word out of a name Post Reply Post New Topic
Author Message
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet Topic: Get the first and last word out of a name
     Posted: 10 Nov 2011 at 1:06am
Hello,

I have a simply CR project that lists the people's names, and their respective info, contained in a DB.
Since there are a lot of items to list, I was wondering if I could make the list horizontally shorter, so that I wouldn't have to make the letter size even smaller.

I tried using a code given as an answer of a post on this forum:


To get first name using
right({table.name},len({table.name})-instrrev({tabla.name},','))


To get last name using
left({table.name},instr({table.name},',')-1)

But when I check the preview, it gives an error. It says the length of chars is less than 0 or it isn't a number, probably because there are no commas in the names of my BD. They're simply like:

João Augusto da Silva Santos
Marco Alexandra Ferreira dos Anjos
Mariana Filipa da Costa
....



other thing I tried, also used as an answer by a post on this forum, was:
For 1st name:

StringVar Array x;
x:=split({Alunos.NomeFo},(if instr({Alunos.NomeFo},",")=0 then " " else ","));
x[1];

and it works perfectly.

Problem is getting the last name... I can't use the same code since the names are as forementioned. First, there are no commas in the names of my Database, 2nd, in Portugal most names have "da", "das", "do", dos" in the middle of the name, and 3rd, of course they are not all of the same size...

Is there still any way I could get the 1st and last name on this case?

Help is appreciated.
Thanks



Edited by Goldberg - 10 Nov 2011 at 1:49am
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 10 Nov 2011 at 2:49am
Okay, so this formula requires that you know all of the pre-last names (da, das, do, dos, etc.).

stringvar sFirst:=split({table.namefield}, ' ')[1];
stringvar sLast:=if (
(' da ' in {
table.namefield})
    or (' das ' in {
table.namefield})
    or (' do ' in {
table.namefield})
    or (' dos ' in {
table.namefield})
//If there are any other known pre-Last Names, enter them here following the examples above.
//Take note that the above have a space before and after the name in the quotes. This is essential for this formula to work
)
        then (if (
(strreverse(split(strreverse({
table.namefield}))[2])='da')
                or (strreverse(split(strreverse({
table.namefield}))[2])='das')
                or (strreverse(split(strreverse({
table.namefield}))[2])='do')
                or (strreverse(split(strreverse({
table.namefield}))[2])='dos')
//Just as before, if you know any other pre-Last Names, enter them here, following the examples from the previous ones.
//Take note that the previous ones do not include spaces before or after the pre-Last Names. This is important for the formula to function properly.

)
                    then strreverse(split(strreverse({
table.namefield}))[2]) & ' ' & strreverse(split(strreverse({table.namefield}))[1])
                        else
             (if (
(strreverse(split(strreverse({
table.namefield}))[3])='da')
                or (strreverse(split(strreverse({
table.namefield}))[3])='das')
                or (strreverse(split(strreverse({
table.namefield}))[3])='do')
                or (strreverse(split(strreverse({
table.namefield}))[3])='dos')
//Just as before, if you know any other pre-Last Names, enter them here, following the examples from the previous ones.
//Take note that the previous ones do not include spaces before or after the pre-Last Names. This is important for the formula to function properly.
)
                    then strreverse(split(strreverse({
table.namefield}))[3]) & ' ' & strreverse(split(strreverse({table.namefield}))[2]) & ' ' & strreverse(split(strreverse({table.namefield}))[1])))
                        else strreverse(split(strreverse({
table.namefield}))[1]);

sFirst & ' ' & sLast;


The only issue I see with this formula is if the Last Name is longer than 3  words (da Silva Santos Santana). You would then need to add another section such as:

                        else
             (if ((strreverse(split(strreverse({table.namefield}))[4])='da')
                or (strreverse(split(strreverse({
table.namefield}))[4])='das')
                or (strreverse(split(strreverse({
table.namefield}))[4])='do')
                or (strreverse(split(strreverse({
table.namefield}))[4])='dos'))
                    then strreverse(split(strreverse({
table.namefield}))[4]) & ' ' & strreverse(split(strreverse({table.namefield}))[3]) & ' ' & strreverse(split(strreverse({table.namefield}))[2]) & ' ' & strreverse(split(strreverse({table.namefield}))[1])))

I know it isn't the prettiest thing, but it works. The problem though is you have to know the pre-Last Names, which you may not know every single one. That is the main flaw in the formula. I hope that this at least helps for a while.


Edited by FrnhtGLI - 10 Nov 2011 at 2:50am
|< /\ '][' ( )
IP IP Logged
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet Posted: 11 Nov 2011 at 1:18am
Oh, man, by saying "most names have 'do', 'dos' (...) I made it a confusion.

I didn't mean it that I meant to show the pre-names, I meant just that I couldn't use the code I said because it would make it show like "Marco da" , ou "João da" ...

What I really meant was I wanted to show the First and Last Name.

By seeing the impressive code that you posted, I easily understood that it would work just by using:

Split({Alunos.NomeFo}, ' ')[1] & " " & strreverse(split(strreverse({Alunos.NomeFo}))[1])

Still, it was thanks to you that I could get this as a result.

I'm really sorry I mis-explained what I mean. On the other hand, I'm really thankfull for your answer and I'll will keep that code saved for future use.

Once again, thanks, FrnhtGLI :)

IP IP Logged
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet Posted: 18 Dec 2011 at 10:57pm
Still concerning this matter,
How would I do if I wanted to turn a, let's say,
 
"Alexandre Ribeira da Silva Lopes" into a "Alexandre Ribeira Silva Lopes"?

In other words, how would I make if I meant to omit the "do", "das" in the names... ?

Thanks
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum