SQL Server 2022 IGNORE NULLS
LAG, LEAD, FIRST_VALUE and LAST_VALUE gained IGNORE NULLS in SQL Server 2022, which makes forward fill a single window call.
- 카테고리
- 창
- Returns
type of the expression- In this category
- 73 of 79
구문
FIRST_VALUE(x) IGNORE NULLS OVER (ORDER BY id) | LAST_VALUE(x) IGNORE NULLS OVER (...)
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | 예 | 2022+ and Azure SQL; earlier versions reject the keyword |
| PostgreSQL | Not built in | not supported through PostgreSQL 18 |
| MySQL | Not built in | not supported |
| SQLite | Not built in | not supported |
| Oracle | 예 | long-standing support; accepts the SQL Server placement and also x IGNORE NULLS inside the parentheses |
예시
SQL Server
-- forward-fill the last known price
SELECT day, price,
LAST_VALUE(price) IGNORE NULLS OVER (
ORDER BY day
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS last_known
FROM prices;
알아 두면 좋은 정보
- SQL Server accepts IGNORE NULLS only after the closing parenthesis: FIRST_VALUE(x) IGNORE NULLS OVER (...). FIRST_VALUE(x IGNORE NULLS) is an Oracle spelling and fails here; Oracle accepts both.
- On LAG and LEAD it means 'the nearest non-NULL row', not 'one row back': LAG(x) IGNORE NULLS OVER (ORDER BY d) is the previous known value. The same 2022 release added the WINDOW clause for naming windows.
- Before 2022 (and on every other engine lacking the clause), the COUNT/MAX emulation or a correlated last-non-NULL subquery replaces it.
Questions about SQL Server 2022 Window Additions
Which SQL Server version added IGNORE NULLS?
SQL Server 2022 (and Azure SQL). On 2012-2019 the parser rejects the keyword; use the COUNT/MAX emulation or an OUTER APPLY (SELECT TOP 1 ... WHERE x IS NOT NULL ORDER BY d DESC) lookup.
Why does my IGNORE NULLS syntax work on Oracle but not SQL Server?
You wrote it inside the parentheses, LAST_VALUE(x IGNORE NULLS), which only Oracle accepts. Move it after the closing parenthesis, LAST_VALUE(x) IGNORE NULLS OVER (...); that form runs on both.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.