As SQL Server does not support ignore nulls in first_value
function, how can we get the first non null value in a window without defining a new column?
I tried to use COALESCE()
, however it didn't work as a window function.
As SQL Server does not support ignore nulls in first_value
function, how can we get the first non null value in a window without defining a new column?
I tried to use COALESCE()
, however it didn't work as a window function.
As has been mentioned,
FIRST_VALUE
does supportIGNORE NULLS
(in SQL Server 2022+). As such you can do:Emulating this for
FIRST_VALUE
isn't too difficult without the syntax though, just add aCASE
to yourORDER BY
on your column to order theNULL
values later:This'll force values that are
NULL
to be ordered later and so can only be the first value if all the values areNULL
.