Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CHARINDEX in reverse


CHARINDEX in reverse

Author
Message
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5832 Visits: 11405
Jeff Moden (7/2/2009) Andrew already did exactly what I would do...

I would not.

There was a member of Led Zeppelin, John Paul Jones.
Would not be happy seeing his name after such "parsing".
nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
Sorry, did the same as Jeff, didn't read Andrews answer and posted the same solution. Hehe

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
...twice !! dozy ******

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Terri-92562
Terri-92562
SSC Veteran
SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)

Group: General Forum Members
Points: 248 Visits: 1242
andrewd.smith (7/1/2009)
REVERSE is one of the less performant string functions in TSQL, so this alternative expression calls REVERSE only once.

SELECT RIGHT(@nam, CHARINDEX(' ', REVERSE(@nam)) - 1)





Bear in mind that this only returns the last name if the last name contains no spaces. My last name consists of 2 words separated by a space and it is parsing routines such as this that send me mail to the last half of my name or file me under the wrong letter. Of course the real problem is the that the name is not stored in separate fields to begin with.



Terri

To speak algebraically, Mr. M. is execrable, but Mr. C. is
(x+1)-ecrable.
Edgar Allan Poe
[Discussing fellow writers Cornelius Mathews and William Ellery Channing.]
andrewd.smith
andrewd.smith
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1222 Visits: 3232
Of course the real problem is the that the name is not stored in separate fields to begin with.


Agreed.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45086 Visits: 39912
Sergiy (7/5/2009)
Jeff Moden (7/2/2009) Andrew already did exactly what I would do...

I would not.

There was a member of Led Zeppelin, John Paul Jones.
Would not be happy seeing his name after such "parsing".


Sergiy is actually the most correct on this. Full name parsing is dangerous at best especially considering that you can have 3 part last names like Van de Graff or the like. The proper way to do this is to head the data provider in the head with a bat and get them to provide the data in the correct format of last name being it's own column.

I've built a splitter for this type of thing including many of the 3 part names, but no lookup list in the world is going to be able to resolve all the names in the world. The data must be entered correctly in some GUI somewhere.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
misscrf
misscrf
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 0
I found this post in trying to split a full name with first, middle and last. I came up with this solution and thought I would share, in case it can help someone else:


,CASE WHEN LTRIM(RIGHT(@fullname,(len(@fullname)-charindex(',',@fullname)))) LIKE '%.%'
THEN REPLACE(LTRIM(RIGHT(@fullname,(len(@fullname)-charindex(',',@fullname)))),REVERSE(left(REVERSE(@fullname), charindex(' ',REVERSE(@fullname))-1)),'')
ELSE LTRIM(RIGHT(@fullname,(len(@fullname)-charindex(',',@fullname)))) END AS First
,ISNULL(CASE WHEN RIGHT(REVERSE(left(REVERSE(@fullname), charindex(' ',REVERSE(@fullname))-1)),1) = '.'
THEN REVERSE(left(REVERSE(@fullname), charindex(' ',REVERSE(@fullname))-1)) END,'') AS Middle
,LTRIM(RTRIM(LEFT(@fullname, CHARINDEX(',', @fullname)-1))) AS Last


Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8534 Visits: 18130
misscrf (2/3/2016)
I found this post in trying to split a full name with first, middle and last. I came up with this solution and thought I would share, in case it can help someone else:


,CASE WHEN LTRIM(RIGHT(@fullname,(len(@fullname)-charindex(',',@fullname)))) LIKE '%.%'
THEN REPLACE(LTRIM(RIGHT(@fullname,(len(@fullname)-charindex(',',@fullname)))),REVERSE(left(REVERSE(@fullname), charindex(' ',REVERSE(@fullname))-1)),'')
ELSE LTRIM(RIGHT(@fullname,(len(@fullname)-charindex(',',@fullname)))) END AS First
,ISNULL(CASE WHEN RIGHT(REVERSE(left(REVERSE(@fullname), charindex(' ',REVERSE(@fullname))-1)),1) = '.'
THEN REVERSE(left(REVERSE(@fullname), charindex(' ',REVERSE(@fullname))-1)) END,'') AS Middle
,LTRIM(RTRIM(LEFT(@fullname, CHARINDEX(',', @fullname)-1))) AS Last



You should warn that it only works with the following format:
LastName, FirstName MiddleInitial.
If the comma is missing, it might generate errors. If the point for the middle initial is missing, it will give incorrect information. So this is highly dependent on correct format of the name which might be useless in many cases.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45086 Visits: 39912
Jeff Moden (7/6/2009)
Sergiy (7/5/2009)
Jeff Moden (7/2/2009) Andrew already did exactly what I would do...

I would not.

There was a member of Led Zeppelin, John Paul Jones.
Would not be happy seeing his name after such "parsing".


Sergiy is actually the most correct on this. Full name parsing is dangerous at best especially considering that you can have 3 part last names like Van de Graff or the like. The proper way to do this is to head the data provider in the head with a bat and get them to provide the data in the correct format of last name being it's own column.

I've built a splitter for this type of thing including many of the 3 part names, but no lookup list in the world is going to be able to resolve all the names in the world. The data must be entered correctly in some GUI somewhere.


It works only for a specific style (format) of name. That's the problem with trying to split "free text" columns, especially names.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search