Skip to content
Advertisement

Update column based on existence in another column

I have two SQL tables. I am trying to conditionally update the second based on detecting existence of a value, URL in the this example, in the first.

The first table, Applications, has the following data:

URL
-------------------------     
http://stackoverflow.com        
http://stackoverflow.com       

In the second table, Modernize, I have the following data:

URL                            FieldToUpdate
--------------------------------------------
http://stackoverflow.com        
http://stackoverflow.com        

The expected result would be if a URL is detected in the first table, Application, that in the FieldToUpdate it would set the text “Yes”. Otherwise, it would set FieldToUpdate to No.

Advertisement

Answer

Use join:

update t
    set fieldtoupdate = (case when f.url is null then 'N' else 'Y' end)
from secondtable t left join
     firsttable f
     on t.url = f.url
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement