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 2005 Forums
 Transact-SQL (2005)
 code error

Author  Topic 

tomus2008
Starting Member

1 Post

Posted - 2010-09-25 : 04:37:56
hi
i write the code for splitting name into first middle and last name and update it into the same table , but it shows error
the table idname contain five coloumn id name fname mname and lname
i pass onlu id and name from front end to table
create trigger tr1 on idname
for insert
as
begin
DECLARE @I1 int,
@I2 int,
@I3 int

UPDATE U
SET
@I1 = CHARINDEX(' ', [name] + ' ')
, [fname] = LEFT( [name], @I1-1)
, @I2 = NullIf(CHARINDEX(' ', [name] + ' ', @I1+1), 0)
, [mname] = SUBSTRING( [name], @I1+1, @I2-@I1-1)
, @I3 = NullIf(CHARINDEX(' ', [name] + ' ', @I2+1), 0)
, [lname] = SUBSTRING( [name], @I2+1, @I3-@I2-1)
FROM dbo.idname AS U
end

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-25 : 12:37:47
how will you determine tha parts of name? will format of name be consistent?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-25 : 13:58:56
Also you should join inserted on id instead of updating the whole table for each insert.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -