Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, November 3, 2014 4:30 PM
Points: 4,574, Visits: 8,366
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: Friday, November 21, 2014 5:17 AM
Points: 1,180, Visits: 2,672
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: Friday, November 21, 2014 5:17 AM
Points: 1,180, Visits: 2,672
...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-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:33 PM
Points: 35,584, Visits: 32,173
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

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

Add to briefcase ««12

Permissions Expand / Collapse