Splitting Full name data to First, Last

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 2
Reputation: Yamahab is an unknown quantity at this point 
Solved Threads: 0
Yamahab Yamahab is offline Offline
Newbie Poster

Splitting Full name data to First, Last

 
0
  #1
Jan 6th, 2007
I'd like to split a full name field on a space into first name last and update my database, I would prefer to stick to TSQL/SQL if it can be done on SQL Server 2005, any ideas?

table actors

actor_id int Unchecked
actor_name varchar(50) Checked
rating smallint Checked
actor_last_name varchar(50) Checked


DECLARE
@position int,
@fullname varchar(50),
@lastname varchar(50)
DECLARE
Names CURSOR FOR
SELECT actor_name, actor_last_name
FROM actors;
OPEN Names;
FETCH NEXT FROM Names INTO @fullname, @lastname;
WHILE @@FETCH_STATUS = 0
BEGIN
set @position=CHARINDEX(" ",@fullname) -- gives Invalid Column name
if(@position>0)
begin
select COUNT(*) from ratings -just needed something here to compile
end
FETCH NEXT FROM Names
END
CLOSE Names
DEALLOCATE Names

SELECT actor_name, actor_last_name
FROM actors;
GO
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7
Reputation: RobStocks is an unknown quantity at this point 
Solved Threads: 0
RobStocks RobStocks is offline Offline
Newbie Poster

Re: Splitting Full name data to First, Last

 
0
  #2
Jan 7th, 2007
Originally Posted by Yamahab View Post
I'd like to split a full name field on a space into first name last and update my database, I would prefer to stick to TSQL/SQL if it can be done on SQL Server 2005, any ideas?

table actors

actor_id int Unchecked
actor_name varchar(50) Checked
rating smallint Checked
actor_last_name varchar(50) Checked


DECLARE
@position int,
@fullname varchar(50),
@lastname varchar(50)
DECLARE
Names CURSOR FOR
SELECT actor_name, actor_last_name
FROM actors;
OPEN Names;
FETCH NEXT FROM Names INTO @fullname, @lastname;
WHILE @@FETCH_STATUS = 0
BEGIN
set @position=CHARINDEX(" ",@fullname) -- gives Invalid Column name
if(@position>0)
begin
select COUNT(*) from ratings -just needed something here to compile
end
FETCH NEXT FROM Names
END
CLOSE Names
DEALLOCATE Names

SELECT actor_name, actor_last_name
FROM actors;
GO
Hi Yamahab,
It appears that you are already on the right track. The invalid column name is due to the use of double quotes rather than single quotes - something I do often when writing SQL.
e.g.
set @position=CHARINDEX(' ',@fullname)
should resolve the error
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 2
Reputation: Yamahab is an unknown quantity at this point 
Solved Threads: 0
Yamahab Yamahab is offline Offline
Newbie Poster

Re: Splitting Full name data to First, Last

 
0
  #3
Jan 9th, 2007
Thank you, I guess I'm used to php where I can freely switch.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 44
Reputation: MCP is an unknown quantity at this point 
Solved Threads: 3
MCP MCP is offline Offline
Light Poster

Re: Splitting Full name data to First, Last

 
0
  #4
Jan 9th, 2007
I think you're better off avoiding cursors where possible! It's much slower than a straight up statement, such as:

  1. SELECT name,
  2. substring(name,1,charindex(' ', actor_name)-1) FirstName,
  3. substring(name,charindex(' ', actor_name)+1,len(actor_name)) LastName
  4. FROM actors
to do an update, you could do:

  1. UPDATE actors
  2. SET first_name = substring(name,1,charindex(' ', actor_name)-1),
  3. last_name = substring(name,charindex(' ', actor_name)+1,len(actor_name))
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC