Search

NVL, COALESCE, NULLIF

NVL, ISNULL
โ€ข
NVL ๋˜๋Š” ISNULL(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2) : ํ‘œํ˜„์‹1 ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด NULL ์ด๋ฉด ํ‘œํ˜„์‹2์˜ ๊ฐ’์„ ์ถœ๋ ฅ
COALESCE, NULLIF
โ€ข
COALESCE(Expression1, Expression2, โ€ฆ):
โ—ฆ
์ฒซ๋ฒˆ์งธ ์ธ์ž์˜ ๊ฐ’์ด NULL ์ด๋ฉด ๋‘๋ฒˆ์งธ ์ธ์ž๊ฐ’์„ ๋ฆฌํ„ด, ๋ชจ๋“  ์ธ์ž๊ฐ€ NULL ์ด๋ฉด NULL ์„ ๋ฆฌํ„ด
โ—ฆ
NULL๊ฐ’ โ†’ ๋‹ค๋ฅธ ๊ฐ’ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
-- NULL์ธ ๊ฒฝ์šฐ 1์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด SELECT value, COALESCE(value, 1) FROM hajuny129.test_table;
Python
๋ณต์‚ฌ
value coalesce None 1 1 1 1 1 0 0 0 0 4 4 3 3
Python
๋ณต์‚ฌ
โ€ข
NULLIF(ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)
โ—ฆ
ํ‘œํ˜„์‹1์ด ํ‘œํ˜„์‹2์™€ ๊ฐ™์œผ๋ฉด NULL์„, ๊ฐ™์ง€ ์•Š์œผ๋ฉด ํ‘œํ˜„์‹1 ์„ ๋ฆฌํ„ด.
โ—ฆ
ํŠน์ • ๊ฐ’ โ†’ NULL ์น˜ํ™˜ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ.
%%sql -- 0์„ null๋กœ ์น˜ํ™˜ SELECT value, 100/NULLIF(value, 0) FROM hajuny129.test_table;
Python
๋ณต์‚ฌ
value ?column? None None 1 100 1 100 0 None 0 None 4 25 3 33
Python
๋ณต์‚ฌ
โ—ฆ
Divide by zero error
โ†’ null๋กœ ๋‚˜๋ˆ„๊ฒŒ ํ•ด์„œ none์ด return๋˜๊ฒŒ ํ•œ๋‹ค