Home > database >  How to fill null values of a column with the values of the same row but different column (sql server
How to fill null values of a column with the values of the same row but different column (sql server

Time:09-24

I have a table and want to "copy" the values of column findoc and insert them in the rows of column findocs which have null values.

table

CodePudding user response:

You can use COALESCE() to take the first non-NULL value:

UPDATE dbo.tablename SET findocs = COALESCE(findocs, findoc);

Though a more efficient way would be to filter:

UPDATE dbo.tablename SET findocs = findoc 
  WHERE findocs IS NULL AND findoc IS NOT NULL;

CodePudding user response:

You can use coalesce() in a query:

select coalesce(finddocs, finddoc) sa finddocs, finddoc, sum
from t;

Or to change the values using update:

update t
    set finddocs = finddoc
    where finddocs is null;
  • Related