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 |
bendertez
Yak Posting Veteran
94 Posts |
Posted - 2007-09-08 : 15:13:26
|
Hi GuysI imagine this is a relatively simple task , but how do you split one single fullname field in to two fields i.e first and last for all records in sql server?i.efullnamefieldjoe bloggspete smithin toFirstnamefield joepeteLastnamefieldbloggssmithI know it involves substring and instring but not sure how to do it.Thank you |
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2007-09-08 : 15:21:25
|
search the script forum there i a function called fnsplittext, i think, which you can use--------------------keeping it simple... |
 |
|
Zoroaster
Aged Yak Warrior
702 Posts |
Posted - 2007-09-09 : 22:34:25
|
quote: Originally posted by bendertez Hi GuysI imagine this is a relatively simple task , but how do you split one single fullname field in to two fields i.e first and last for all records in sql server?i.efullnamefieldjoe bloggspete smithin toFirstnamefield joepeteLastnamefieldbloggssmithI know it involves substring and instring but not sure how to do it.Thank you
Here:SELECT SUBSTRING(FULLNAMEFIELD, 0, CHARINDEX(' ',FULLNAMEFIELD)) AS FIRSTNAMEFIELD, SUBSTRING(FULLNAMEFIELD, CHARINDEX(' ', FULLNAMEFIELD)+1,100) AS LASTNAMEFIELDFROM TABLE Future guru in the making. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-09-10 : 02:58:18
|
What about:Mary Ann Smith ? |
 |
|
bendertez
Yak Posting Veteran
94 Posts |
Posted - 2007-09-10 : 04:36:07
|
Thanks ZoroasterThat seems to do the trick, however the actual field i'm looking at is a middle name field and some people have a middle name and some people don't.At the moment the results are being returned as followsForename:Anne Marieis split as follows:Firstnamefield:AnneMiddlenameField:MarieWhich is what I want, however if the person does not have a middle name i.e.Foreneme:Johnit returns:Firstnamefield:NULLMiddlenamefield:JohnI want this to be returned the other way around.i.e.Firstnamefield:JohnMiddlenamefield:NULLAny ideas?Thank you |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|