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
 General SQL Server Forums
 New to SQL Server Programming
 Extract a surname

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-08-18 : 05:28:36
I have inherited a badly design database where the official's name is stored as

A Brownwell,
Jane Smith
B Bloggs

Is there a way I can sort by surname ?

Devart
Posting Yak Master

102 Posts

Posted - 2010-08-18 : 05:49:24
Hi,

Try this:

substring(<your_field_name>,patindex('% %',<your_field_name>)+1,datalength(<your_field_name>))

Best regards,


Devart,
SQL Server Tools:
dbForge Schema Compare
dbForge Data Compare
dbForge Query Builder
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-08-18 : 06:27:08
Thanks for that - I wanted to use it in a Crystal formula and it won't work in that...... it works in a sql expression.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-18 : 07:52:07
This is a relatively common problem and I haven't seen any fool proof solutions. If your column only has first name and last name then you can always split it in two. Unless the first name is Oscar and last name is de la Hoya. Or Michael J. Fox or etc., etc.

This works pretty well splittnig up the names

SELECT PARSENAME(REPLACE ('A Brownwell',' ','.'),1)
,PARSENAME(REPLACE ('A Brownwell',' ','.'),2)


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-08-18 : 08:51:50
I think parseName is limited to 4 parts so be warned. Example:

DECLARE @foo VARCHAR(50)

SET @foo = 'a b c d e'

SELECT PARSENAME(REPLACE(@foo, ' ', '.'), 1)

SET @foo = 'a b c d'

SELECT PARSENAME(REPLACE(@foo, ' ', '.'), 1)


There's no good way to do this. If the data is as you describe and there can be any number of different 'words' inside the string then you are always going to have to do some manual viewing of the data to check it.

You could probably automate patterns. Say 1 character then a space then a word -- the word is probably the surname

That might get rid of most of the data.

Then inspect the rest by hand (a PITA I Know)


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-18 : 10:22:27
...and the answer is....

WHATEVER technical solution you come up with..20% will need to be fixed manually



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -