I have a table in my database where I store addresses. The address can be many lines (1 to 6)
Each line of the address is stored as a separate column, up to 6 columns.
I now want to loop through each row in my table and find the value of the last NOT NULL column. This could be column 3,4,5 or 6.
I need a SQL command where given a row I get the value of the last column which is not null.
It depends a little on what you mean by last not-null column. I’m assuming you mean ‘given this list, return the first one with a value’, where the list is [6, 5, 4, 3]. If so, use this:
The
COALESCE()function returns the first value that resolves to not null, or null if all values are null.