user3386779
user3386779

Reputation: 7175

update table field with another field value if the value if blank in postgres

I have the table with two columns companyName and accountName.I want to check the the company value is empty and update the field with value accountName in the same row

testTable
id companyName accountName
1   a            a1
2                b1         //update with b1 in companyNAme field
3   c            c1
4                d1        //update with  d1 in companyNAme field

I want to check if the companyName field is empty then update with companyName with accountName field value .From the abobe example I want to update id 2 companyName name with value b1 and id 4 companyName name with value d1 in postgres.

Upvotes: 1

Views: 1224

Answers (1)

Jeremy
Jeremy

Reputation: 6723

It's just a simple update:

UPDATE "testTable" 
SET "companyName" = "accountName"
WHERE "companyName" IS NULL;

It would be better to not have capitalization in the column and table names, to avoid requiring those quotation marks.

Upvotes: 4

Related Questions