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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 sql statement help

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2004-10-25 : 10:11:59
I have a field called fullname,firstname,and lastname.

Can someone help me with a sql statement to take fullname and put it into firstname and lastname. (Parse it by the last space)

samsekar
Constraint Violating Yak Guru

437 Posts

Posted - 2004-10-25 : 10:26:20
Declare @fullname varchar(20), @firstname varchar(10), @lastname varchar(10), @sep int
Set @fullname = 'Sir ABC xyz'
Select @fullname = reverse(@fullname)
Set @sep = charindex(' ', @fullname)
Select @firstname = reverse(substring(@fullname,1, @sep-1))
Select @lastname = reverse(substring(@fullname, @sep, len(@fullname)))
Select @firstname, @lastname

- Sekar
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2004-10-25 : 10:32:01
How do I change this to a select from users - and change the fullname from the users table to firstname and lastname in users.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-10-25 : 10:43:02
update MyTable
set firstName = reverse(substring(fullname,1, charindex(' ', fullname) - 1))
set lastname = reverse(substring(fullname, charindex(' ', fullname), len(fullname)))


Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -