Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Picking out last name out of a string

Author  Topic 

umertahir
Posting Yak Master

154 Posts

Posted - 2010-01-08 : 12:04:48
How do I pick out last name of a string of text?

e.g., Varchar text is : 'Morgan K Lens'

I want to pick up 'lens' from the first example.

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2010-01-08 : 12:07:00
Try this:
http://dl.dropbox.com/u/2507186/Functions/FormatName.sql

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

umertahir
Posting Yak Master

154 Posts

Posted - 2010-01-08 : 12:11:23
I have tried the following code:


SELECT RIGHT('MORGAN K LENS', CHARINDEX(' ', REVERSE('MORGAN K LENS')))


Result:
LENS
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-08 : 12:16:33
quote:
Originally posted by umertahir

I have tried the following code:


SELECT RIGHT('MORGAN K LENS', CHARINDEX(' ', REVERSE('MORGAN K LENS')))


Result:
LENS


this will work as long as your name format is consistent with at least 2 parts
Go to Top of Page
   

- Advertisement -