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 2008 Forums
 Transact-SQL (2008)
 How to update table from column to column in table

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2012-12-27 : 11:23:48
hello all,

i have a table in which i have 2 columns code and description

in first column i have data like
1 - Prevention
2 - Respiratory
3 - Cardiovascular

in 2nd column

NULL
NULL
NULL

i need to update

only Prevention,
Respiratory,
Cardiovascular

into 2 nd column

i have written query like this :

update ECT_HEDIS_SubDomain set SubDomainDescription = b.SubDomainDescription
from ECT_HEDIS_SubDomain b
INNER JOIN ECT_HEDIS_SubDomain a
on substring(a.ECTSubDomainCode,1,4) = b.ECTSubDomainCode

suggest me

P.V.P.MOhan

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2012-12-27 : 11:33:51
Please check your example code as it looks like you are joining the table to itself. Also this will only update where ECTSubDomainCode is four characters or less.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-27 : 11:48:58
Shouldn't it be like this

update ECT_HEDIS_SubDomain 
set ECTSubDomainCode = left(ECTSubDomainCode,Charindex(' - ',ECTSubDomainCode)-1)
SubDomainDescription = Substring(ECTSubDomainCode,Charindex('-',ECTSubDomainCode) + 2,len(ECTSubDomainCode))
Go to Top of Page
   

- Advertisement -