Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a column up .... 1

Status
Not open for further replies.

rabinski

Programmer
May 13, 2005
21
GB
Hi guys -

I have a column with 'Mr Joe Bloggs' and need to split into Title,Forename and Surname ...

Coming from a perl backgound I would use regex - but anyway I am looking at CHARINDEX and SUBSTRING -

I just need to split on the space .....

Any helpful tips would be most excellent !

Thanks in advance ...

R
 
You're probably right. Other than PATINDEX (which only returns the first instance of a pattern, these would be the two to use.

So, maybe some code like:

Code:
Declare @Space1 int,
        @Space2 int,
        @Title varchar(4),
        @FName varchar(25),
        @LName varchar(25)

Set @Space1 = CHARINDEX(' ',NAME,0)
Set @Space2 = CHARINDEX(' ',Name,@Space1+1)
Set @Title = Substring(NAME,0,@Space1)
Set @FName = Substring(NAME,@Space1+1,@Space2)
Set @LName = Substring(NAME,@Space2,<Value>)

<Value> should be the actual column size. Like 50 if NAME col. is a char(50) datatype.

Does this help?






Catadmin - MCDBA, MCSA
"If a person is Microsoft Certified, does that mean that Microsoft pays the bills for the funny white jackets that tie in the back???
 
HEY! Thanks, jbenson001! I appreciate you posting the link.



Catadmin - MCDBA, MCSA
"If a person is Microsoft Certified, does that mean that Microsoft pays the bills for the funny white jackets that tie in the back???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top