user1913919
user1913919

Reputation: 53

SQL Server - Pull data from one row and update another

I have a table in SQL server with the following rows and columns (A-code, Desc, Length and B-code)

A-Code Desc Length B-Code

A100 Rural-3D 25 A100

A100 NULL 55 B100

** In the first row A-code = B-Code

** In the second A-code<> B-Code and Desc is NULL

What I want to do is update the Desc column of the second row (A-code<> B-Code) with the Desc value from the first row where the A-Code = B-Code

So that my final table result would be as follows

A-Code Desc Length B-Code

A100 Rural-3D 25 A100

A100 Rural-3D 55 B100

Please help .

Thanks

Upvotes: 1

Views: 889

Answers (1)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

UPDATE a
SET a.[Desc] = b.[Desc]
FROM MyTable A
INNER JOIN MyTable B on a.[A-Code] = b.[A-Code] and b.[A-Code] = b.[B-Code]
WHERE a.[A-Code] <> a.[B-Code]
And a.[Desc] IS NULL;

SqlFiddle

Upvotes: 4

Related Questions