Anjar Syaefa

Selasa, 16 Mei 2017

Mengganti value Null dengan value sebelumnya SQL Server

Mengganti value Null dengan value sebelumnya SQL Server

DECLARE @Table TABLE(
ID INT,        
Val INT )

INSERT INTO @Table (ID,Val) SELECT 1, 3
INSERT INTO @Table (ID,Val) SELECT 2, NULL
INSERT INTO @Table (ID,Val) SELECT 3, 5
INSERT INTO @Table (ID,Val) SELECT 4, NULL
INSERT INTO @Table (ID,Val) SELECT 5, NULL
INSERT INTO @Table (ID,Val) SELECT 6, 2

SELECT *, ISNULL(Val, (SELECT TOP 1 Val FROM @Table WHERE ID < t.ID AND Val IS NOT NULL ORDER BY ID DESC))

FROM @Table t


sumber: http://stackoverflow.com/questions/1345065/sql-query-replace-null-value-in-a-row-with-a-value-from-the-previous-known-value

Tidak ada komentar:

Posting Komentar