SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 column with part of the value from other
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rajendrarama
Starting Member

14 Posts

Posted - 05/31/2012 :  12:17:44  Show Profile  Reply with Quote
I have a table with

ORDERID _NAME CUSTOMER_SURNAME

1 MR/MRS BADACH
2 MR ROCHFORD & MISS HENDERSON
3 MR/MRS PATERSON
4 MISS JONES & MISS QUINLIVAN
5 MR/MRS BRADLEY
6 MR/MRS CLEECE
7 MR/MRS FOWLER
8 D JONES
9 K COUCHLIN
10 CLARKE
11 L WRIGHT

can we get the last word to be transferred to customer_surname column(it's blank for now)
with the _name being the same

_NAME CUSTOMER_SURNAME
MR/MRS BADACH BADACH
MR ROCHFORD & MISS HENDERSON HENDERSON
MR/MRS PATERSON PATERSON
MISS JONES & MISS QUINLIVAN QUINLIVAN
MR/MRS BRADLEY BRADLEY
MR/MRS CLEECE CLEECE
MR/MRS FOWLER FOWLER
D JONES JONES
K COUCHLIN COUCHLIN
CLARKE CLARKE
L WRIGHT WRIGHT

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 05/31/2012 :  13:18:00  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
update tbl
set CUSTOMER_SURNAME = right(_NAME, len(_NAME)-charindex(' ',reverse(_NAME))-1)
where _NAME like '% %'


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Skorch
Constraint Violating Yak Guru

USA
295 Posts

Posted - 05/31/2012 :  14:48:03  Show Profile  Reply with Quote
Nigel, yours didn't quite work as far as I could tell. There probably is a more elegant solution, but this should work for the sample provided:


declare @t table (ORDERID int, _NAME varchar(30), CUSTOMER_SURNAME varchar(20))
insert @t (ORDERID, _NAME)

select 1, 'MR/MRS BADACH' union
select 2, 'MR ROCHFORD & MISS HENDERSON' union
select 3, 'MR/MRS PATERSON' union
select 4, 'MISS JONES & MISS QUINLIVAN' union
select 5, 'MR/MRS BRADLEY' union
select 6, 'MR/MRS CLEECE' union
select 7, 'MR/MRS FOWLER' union
select 8, 'D JONES' union
select 9, 'K COUCHLIN' union
select 10, 'CLARKE' union
select 11, 'L WRIGHT'

update @t
set customer_surname = 
case
	when charindex(' ', _name) > 0 then reverse(substring(reverse(_name), 0, charindex(' ', reverse(_name)))) 
	else _name
end
from @t

select * from @t


Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/31/2012 :  15:29:34  Show Profile  Reply with Quote

update @t
set customer_surname = REVERSE(STUFF(REVERSE(_NAME),CHARINDEX(' ',REVERSE(_NAME)),LEN(_NAME),''))


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

Go to Top of Page

Skorch
Constraint Violating Yak Guru

USA
295 Posts

Posted - 05/31/2012 :  15:54:59  Show Profile  Reply with Quote
The above still doesn't account for case #10 where you get a NULL

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/31/2012 :  16:06:13  Show Profile  Reply with Quote
quote:
Originally posted by Skorch

The above still doesn't account for case #10 where you get a NULL

Some days you're the dog, and some days you're the fire hydrant.



update @t
set customer_surname = REVERSE(STUFF(REVERSE(_NAME),CHARINDEX(' ',REVERSE(_NAME) + ' '),LEN(_NAME),''))






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

Go to Top of Page

rajendrarama
Starting Member

14 Posts

Posted - 05/31/2012 :  17:09:46  Show Profile  Reply with Quote
I am still getting the value as null for case #10(CLARKE)
Go to Top of Page

Skorch
Constraint Violating Yak Guru

USA
295 Posts

Posted - 05/31/2012 :  17:45:15  Show Profile  Reply with Quote
Have you tried using what I posted?


update @t
set customer_surname = 
case
	when charindex(' ', _name) > 0 then reverse(substring(reverse(_name), 0, charindex(' ', reverse(_name)))) 
	else _name
end
from @t


Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

rajendrarama
Starting Member

14 Posts

Posted - 06/01/2012 :  04:11:12  Show Profile  Reply with Quote
Thank you. It has updated the table. For any new row insertion in the future,
how can i automatically update the surname column?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 06/01/2012 :  11:25:05  Show Profile  Reply with Quote
quote:
Originally posted by rajendrarama

Thank you. It has updated the table. For any new row insertion in the future,
how can i automatically update the surname column?


for that why not create it as a computed column then?


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

Go to Top of Page

rajendrarama
Starting Member

14 Posts

Posted - 06/04/2012 :  07:31:14  Show Profile  Reply with Quote
Could you please let me know how can i create a computed column such that a new row insertion with _name should automatically fill the surname column with the above logic
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 06/04/2012 :  10:00:14  Show Profile  Reply with Quote
when you create a computed column the column wont store value physically unless it is PERSISTED but it will be calculated on the fly while selecting from table.

So you dont need to (or cant) explicitly insert into computed column.

in your case it will be like

ALTER TABLE tablename ADD customer_surname AS case
	when charindex(' ', _name) > 0 then reverse(substring(reverse(_name), 0, charindex(' ', reverse(_name)))) 
	else _name
end


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

Go to Top of Page

rajendrarama
Starting Member

14 Posts

Posted - 06/06/2012 :  09:57:20  Show Profile  Reply with Quote
If the user has entered a space after the name, then it is not updating the customer_surname. could you please help with it.


Edited by - rajendrarama on 06/06/2012 13:09:21
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/06/2012 :  13:29:09  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
create trigger tr_tbl on tbl for insert, update
as
update tbl
set _name = reverse(substring(reverse(t._name), 0, charindex(' ', reverse(t._name))))
from tbl t
join inserted i
on t.pk = i.pk
where charindex(' ', t._name) > 0

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 06/07/2012 :  22:34:31  Show Profile  Reply with Quote
quote:
Originally posted by rajendrarama

If the user has entered a space after the name, then it is not updating the customer_surname. could you please help with it.




you mean with computed column?

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000