I've been working on a DBO that requires a new code going through.
I have to manually add a specific code to my requirements in a column that has thousands of rows.
The issue is, some are already populated and I need to add them into the next available empty column.
its currently in this format:
I need to add Code Z987 to the first NULL of each ID
](https://i.stack.imgur.com/oUnV8.png)
First, whoever designed this data needs to read up on database normalization, as this design violates the most basic principles. The best solution would be to redesign the data to follow good design practices.
However, if that is not an option, you will need to write an
UPDATEstatement that updates every column, but conditionally chooses either the current or new value as the value to be assigned. The condition would be based on which columns are already set. To avoid duplication of logic, you can use aCROSS APPLYto identify which column is to be updated. That can then be used to feedCASEexpressions in theSETlogic.The result would be something like: