Skip to content
Advertisement

Choose one value from several columns and create a new column with the chosen value in Oracle SQL Developer

I am trying to build a SQL query where I create a new column that contains a chosen value form several columns. The rows of the columns are all null except one which is the desired value to put in the new column. An example could be as the following:

enter image description here

Thank you!

Advertisement

Answer

As promptly commented by jarlh, you can just use coalesce():

select coalesce(substration_fk1, substration_fk2, substration_fk3) res
from mytable

For each row, coalesce() sequentially checks the value of each column in the order in which they are given to the function, and returns the first non-null value.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement