Reputation: 67
In my database i have a table of customer with one field of name which contains the name of the customer(first name and last name) in a single column now i want to write a query that search the first name and update its last name.
i would be appreciate if any one help me out.
thanks.
Upvotes: 0
Views: 1198
Reputation: 10198
This is what i tried
create table employeedemo
(id int, Name varchar(50), Location varchar(20))
insert into employeedemo values('1','Amir khan','India');
insert into employeedemo values('2','Salman khan','India');
insert into employeedemo values('3','Sharuk khan','India');
insert into employeedemo values('4','Saif khan','India');
insert into employeedemo values('5','Amit sarna','India');
insert into employeedemo values('6','Sanjay Dutt','India');
insert into employeedemo values('7','Sunny Deol','India');
update employeedemo
set name= 'Saif pataudi'
where SUBSTRING(Name, 1, CHARINDEX(' ',Name))='Saif'
Note: Name column must follow syntax as (firstname space lastname) n vote up if its help :)
Upvotes: 0
Reputation: 493
This is not as easy as you maybe think. What you if someone has a second firstname? Where to cut?
This is why you normally normalize a database structure a create a column for the firstname and a column for the lastname.
Upvotes: 1