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)
 Copy a Data in a Row then Insert to Another Row

Author  Topic 

leinad28
Starting Member

4 Posts

Posted - 2012-10-17 : 10:06:51
Hi guys,

I'm stuck.Need some help about copying a data in a row then insert this data in another row when the field is blank or null. See sample data below:

MsgType ProcessCode Date TraceNumber InstCode
0200 480000 20121017 000001 9901
0210 480000 20121017 000001 NULL

I need only to copy the data on field InstCode then insert this in NULL row below.

I'm confused if what could be the best way to do it.
Will appreciate a lot for some inputs. Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-17 : 23:40:23
[code]
UPDATE t
SET t.InstCode = t1.InstCode
FROM table t
INNER JOIN table t1
ON t1.ProcessCode = t.ProcessCode
AND t1.Date = t.Date
AND t1.TraceNumber = t.TraceNumber
WHERE t1.InstCode IS NOT NULL
AND t.InstCode IS NULL
[/code]

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

Go to Top of Page
   

- Advertisement -