Olivero SRL
Olivero SRL

Reputation: 5

Replace value of 1 column with value of column 2

I have 2 columns with values like those:

Col1   | Col2

None   |   1
1      |   2
None   |    3

I just want to REPLACE the value of Column 1 if is "None" with value of Column 2

I've searches some questions like this in the Forum but couldn't find anythink like this

Hope you undertood me folks.

Upvotes: 0

Views: 613

Answers (2)

Paul Wildenhain
Paul Wildenhain

Reputation: 1509

This is a good use case for the coalesce() function

select coalesce(Col1, Col2) as Col1 from table_name

NOTE: If the column value is literally the string None and not a NULL datatype, then this will not work.

Upvotes: 0

zedfoxus
zedfoxus

Reputation: 37059

If you want to replace data, you should use update.

Update tablename
Set Col1 = Col2
Where Col1 = 'None';

If you want to write a query that will NOT change the data but will mask a NULL with different information, you could use Paul's answer or if you want to mask 'None' with different information, use Larnu's idea of using case statement:

select case when Col1 = 'None' then Col2 else Col1 end as Col1, Col2
from tablename;

If Larnu posts an answer, you might want to give Larnu the credit.

Upvotes: 1

Related Questions