Skip to content

Instantly share code, notes, and snippets.

@Ninjanaut
Created July 10, 2022 22:58
Show Gist options
  • Select an option

  • Save Ninjanaut/411f9e2e7290363477a5cf75004c78c3 to your computer and use it in GitHub Desktop.

Select an option

Save Ninjanaut/411f9e2e7290363477a5cf75004c78c3 to your computer and use it in GitHub Desktop.
Fill empty values based on the value in previous rows
declare @temp as table (
DayDate date not null,
Inventory int null
)
insert into @temp
values
('01-01-2022', 1234),
('02-01-2022', null),
('03-01-2022', null),
('04-01-2022', 2345),
('05-01-2022', null),
('06-01-2022', 3456),
('07-01-2022', null),
('08-01-2022', null)
select * from @temp
declare @previousValue int
UPDATE @temp
SET
@previousValue = COALESCE(Inventory, @previousValue),
Inventory = COALESCE(Inventory, @previousValue)
select * from @temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment