Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

CHARINDEX in reverse Expand / Collapse
Author
Message
Posted Sunday, July 5, 2009 9:29 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:00 AM
Points: 5,353, Visits: 10,265
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".
Post #747484
Posted Monday, July 6, 2009 8:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 7, 2016 3:51 AM
Points: 1,274, Visits: 2,817
Sorry, did the same as Jeff, didn't read Andrews answer and posted the same solution.

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

Post #747785
Posted Monday, July 6, 2009 8:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 7, 2016 3:51 AM
Points: 1,274, Visits: 2,817
...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

Post #747788
Posted Monday, July 6, 2009 10:12 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, October 3, 2012 8:35 AM
Points: 242, Visits: 1,242
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.]
Post #747870
Posted Monday, July 6, 2009 12:10 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,212, Visits: 3,232
Of course the real problem is the that the name is not stored in separate fields to begin with.


Agreed.
Post #747932
Posted Monday, July 6, 2009 2:54 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 9:39 PM
Points: 40,978, Visits: 38,272
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."

Helpful Links:
How to post code problems
How to post performance problems
Post #748073
Posted Wednesday, February 3, 2016 9:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 3, 2016 9:21 AM
Points: 1, 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

Post #1758160
Posted Wednesday, February 3, 2016 9:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:51 AM
Points: 7,451, Visits: 16,187
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
Post #1758168
Posted Wednesday, February 3, 2016 9:43 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 9:39 PM
Points: 40,978, Visits: 38,272
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."

Helpful Links:
How to post code problems
How to post performance problems
Post #1758169
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse