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.
| Author |
Topic |
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2009-02-17 : 07:51:21
|
I have a table with three columns:Forename MiddleName Surname-------- ---------- -------John Robert SmithDavid Craig RobinsonEric Ian Nicholls Is there a way I can test the first (Forename) field to see if it contains two strings of characters (words) which are each larger than 1 character and then update the table so that the first column is replaced with the first 'word' and the second column is updated with the second 'word'?This would then return:Forename MiddleName Surname-------- ---------- -------John Robert SmithDavid Craig RobinsonEric Ian Nicholls |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-17 : 08:09:26
|
| try thisdeclare @tab table (Forename varchar(32), MiddleName varchar(32), Surname varchar(32))insert into @tab select 'John','Robert','Smith' union allselect 'David Craig',null,'Robinson' union allselect 'Eric Ian',null,'Nicholls'select * from @tabupdate @tabset middlename = right(forename,charindex(' ',reverse(forename),1)),forename = left(forename,charindex(' ',forename,1))where charindex(' ',forename,1) > 0 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-02-17 : 08:12:52
|
| To avoid leading spaceset middlename = right(forename,charindex(' ',reverse(forename),1)-1),MadhivananFailing to plan is Planning to fail |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2009-02-17 : 08:24:52
|
| Many thanks guys. I learned sonething new from that :) |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2009-02-17 : 08:30:23
|
quote: Originally posted by OldMySQLUser ...which are each larger than 1 character...
So you want to leave middle initials in the first name column?________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2009-02-17 : 08:40:34
|
quote: Originally posted by blindman
quote: Originally posted by OldMySQLUser ...which are each larger than 1 character...
So you want to leave middle initials in the first name column?________________________________________________If it is not practically useful, then it is practically useless.________________________________________________
No. But I'm trying to clean up a database table littered with garbage input. I have stuff like 'John & Sue' in the Forename field .I'll have to work through this stuff in small, systematic, stages. Only 7 million records, so I *may* live long enough to do it! |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2009-02-17 : 09:22:17
|
You may find this useful:http://sqlblindman.pastebin.com/f68f37c15For example:declare @tab table (Forename varchar(32), MiddleName varchar(32), Surname varchar(32))insert into @tabselect 'John','Robert','Smith'union all select 'David Craig',null,'Robinson'union all select 'Eric Ian',null,'Nicholls'union all select 'J. Frederick',null,'Peterson III'union all select 'Mrs. Juliet','Anne','Von Kaput'union all select 'Captain James T. Kirk', null, nullunion all select 'Clinton, William', 'Jefferson', nullselect dbo.FormatName(coalesce(Forename, '') + ' ' + coalesce(MiddleName, '') + ' ' + coalesce(Surname, ''), 'F') as FirstName, dbo.FormatName(coalesce(Forename, '') + ' ' + coalesce(MiddleName, '') + ' ' + coalesce(Surname, ''), 'M') as MiddleName, dbo.FormatName(coalesce(Forename, '') + ' ' + coalesce(MiddleName, '') + ' ' + coalesce(Surname, ''), 'L') as LastNamefrom @tab Output:FirstName MiddleName LastNameJohn Robert SmithDavid Craig RobinsonEric Ian NichollsJ Frederick PetersonJuliet Anne Von KaputJames T KirkWilliam Jefferson Clinton ________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2009-02-18 : 04:10:17
|
| That is very useful blindman. Many thanks for taking the time and trouble to help. Much appreciated. |
 |
|
|
|
|
|
|
|