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:
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.