t-sql merging two tables and replace null values

Jennifer Alex

ByJennifer Alex

Dec 13, 2021 , ,

I have these two tables, and that what I want is to compare them to know if there is any null value in table 2, if there is, replace the existing value in table 1 by the null value in table 2 (by the code column that is the primary key).

         Table 1
Code       Name          Points
 1     Juan Perez          10
 2     Marco Salgado       5
 3     Carlos Soto         9
 4     Alberto Ruiz        12
 5     Alejandro Castro    5
 10    Jonatan Polanco     0
 11    JD                 NULL


          Table 2
Code       Name          Points
 1     Juan Perez          10
 2     Marco Salgado       5
 3     Carlos Soto         9
 4     Alberto Ruiz        12
 5     Alejandro Castro    5
 10    Null                0
 11    JD                  9

The resulting table should look like this

         Table 2
Code       Name          Points
 1     Juan Perez          10
 2     Marco Salgado       5
 3     Carlos Soto         9
 4     Alberto Ruiz        12
 5     Alejandro Castro    5
 10    Jonatan Polanco     0
 11    JD                  9