| | |
Splitting Full name data to First, Last
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2007
Posts: 2
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2006
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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
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
•
•
Join Date: Sep 2006
Posts: 44
Reputation:
Solved Threads: 3
I think you're better off avoiding cursors where possible! It's much slower than a straight up statement, such as:
to do an update, you could do:
MS SQL Syntax (Toggle Plain Text)
SELECT name, substring(name,1,charindex(' ', actor_name)-1) FirstName, substring(name,charindex(' ', actor_name)+1,len(actor_name)) LastName FROM actors
MS SQL Syntax (Toggle Plain Text)
UPDATE actors SET first_name = substring(name,1,charindex(' ', actor_name)-1), last_name = substring(name,charindex(' ', actor_name)+1,len(actor_name))
![]() |
Similar Threads
- C++ and data structure tutorial (C++)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- Suggestions for perl resources? Dealing with csv data. (Perl)
- will my 180 GB drive lose data on fresh install of Windows XP? (Windows NT / 2000 / XP)
- Corrupted HDD? (Storage)
- 700mb cd reads full after burning 2.5mb of data (Windows 95 / 98 / Me)
Other Threads in the MS SQL Forum
- Previous Thread: Date Problem
- Next Thread: Data Replication
Views: 8991 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday





