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.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2007-03-19 : 09:23:16
|
jumoke writes "I have 2 tables ownerDet_tb with ownerDet_id and ownerDet_nameownerDet_id ownerDet_name1 INDIVIDUAL2 COMPANY3 JOINT:COUPLE4 JOINT:IND_COMPANY5 EMBASSY6 LOCAL GOVERNMENT7 STATE GOVERNMENT8 FEDERAL GOVERNMENT The Second table is the owner table which will have ownerDet_id. Hi based on the table above ,I am to autogenerate owner_id based in ownerDet_Desc.E.g if ownerDet_Desc is embassy……….the owner_id shld be say emb001And individual is ind001 etc etc tecYou get idea?" |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-19 : 09:37:26
|
[code]declare @ownerDet_tb table( ownerDet_id int, ownerDet_name varchar(20))insert into @ownerDet_tbselect 1, 'INDIVIDUAL' union allselect 2, 'COMPANY' union allselect 3, 'JOINT:COUPLE' union allselect 4, 'JOINT:IND_COMPANY' union allselect 5, 'EMBASSY' union allselect 6, 'LOCAL GOVERNMENT' union allselect 7, 'STATE GOVERNMENT' union allselect 8, 'FEDERAL GOVERNMENT'declare @owner table( owner_id varchar(10), ownerDet_id int)insert into @ownerselect owner_id = lower(left(ownerDet_name, 3)) + right('000' + convert(varchar(3), (select count(*) from @ownerDet_tb x where left(x.ownerDet_name, 3) = left(o.ownerDet_name, 3) and x.ownerDet_name <= o.ownerDet_name)), 3), ownerDet_idfrom @ownerDet_tb oorder by left(ownerDet_name, 3)select * from @owner[/code] KH |
 |
|
|
|
|
|
|