r/SQL • u/FlucDissThm • May 21 '26
SQL Server Best way to update NULL values?
I have data that looks like this:
| Col1 | Col2 | Col3 |
|---|---|---|
| A | Two | 3 |
| B | Two | NULL |
| C | Two | NULL |
| D | Five | NULL |
| E | Five | 6 |
Working in SQL Server, what's the best way to update the NULL values in Col3 to the only non-null value associated with equal values in Col2? e.g. I'd want to update this table to read
| Col1 | Col2 | Col3 |
|---|---|---|
| A | Two | 3 |
| B | Two | 3 |
| C | Two | 3 |
| D | Five | 6 |
| E | Five | 6 |
9
Upvotes
8
u/Rumborack17 May 21 '26 edited May 21 '26
Should just be
update table set col3 = (select top 1 col3 from table t2 where t2.col2 = table.col2 and t2.col3 is not null) where col3 is null
Assuming that a certain col2 value always has the same col3 value. If that is not the case you need to decide on how to order them (or more generally on how to decide what value to take).
Edit: fixed typos
5
u/FastLikeACheeta May 22 '26
Not a direct answer to the question, you already have a few suggestions for that.
I’m curious what happened to make your dataset this way. Would this update be a fix to some sort of underlying bug, not truly fixing the core issue?
If not some sort of flaw elsewhere, then there’s gotta be some meaning to why these other rows have NULL values. I’d perhaps create a new table mapping the non-null values, then join the two together to have the result of non-null while keeping the original tables data intact
CREATE NewTable (Col2 <w/e data type>, Col3 <w/e data type); INSERT INTO NewTable (Col2, Col3) SELECT Col2, Col3 FROM OldTable WHERE Col3 IS NOT NULL;
CREATE VIEW vw_SomeName AS SELECT old.Col1, old.Col2, new.Col3 FROM OldTable old INNER JOIN NewTable new ON old.Col2 = new.Col2
5
u/VladDBA SQL Server DBA May 21 '26 edited May 21 '26
Just based on your example alone
Obviously, this may get more convoluted depending on the rest of the values in Col2
Edited to add: if you have more values on Col2, then an UPDATE using an INNER JOIN would be a better option:
2
u/Neat_Treat2414 May 21 '26
How many values do you have in Column 2? You could use CASE statements to update Col3 if there aren’t a ton of distinct values in Col2
4
u/mbrmly May 21 '26 edited May 21 '26
You can make a CTE Along the lines of With distinctvalues as (select distinct col2 ,col3 from yourtable where col3 is not null)
Select col 1, col2, d.col3 from yourtable Y Left join distinctvalues D On d.col2 = y.col2
This should work
4
u/nIBLIB May 21 '26
Are you certain there’s only one value per col2?
Update TABLE FROM (Select distinct Col2, col3 from table where col 3 is not null) t2 Set Col3 = t2.Col3 Where table.Col2 =t2.col2
2
u/Imaginary__Bar May 21 '26
This might not be the best way but I'd create a new calculated col_4, rename col_3 to col_3_old, and rename col_4 to col_3.
Once I was happy then I'd delete col_3_old
I'm sure there's a better/safer way, especially if your table is in PROD and getting hit repeatedly while you're trying to make the change, but I'm guessing that's not the case.
1
u/Large_Hedgehog2416 May 25 '26
Does the front end use inline sql to insert the data or does it call stored procedures? If so you could possibly modify the SPs to validate the input on the col3 nulls and set it based n the value they insert into col2, no?