๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Computer Science/๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค] NULL ๊ด€๋ จ ํ•จ์ˆ˜ (NVL, NVL2, ISNULL, NULLIF, COALESCE) ์˜ˆ์ œ ํฌํ•จ, ์‰ฝ๊ฒŒ ๊ธฐ์–ตํ•˜๋Š” ๋ฐฉ๋ฒ•

by Jay Din 2024. 8. 21.
728x90
๋ฐ˜์‘ํ˜•

์‰ฝ๊ฒŒ ๊ธฐ์–ตํ•˜๋Š” ๋ฐฉ๋ฒ•

1. NVL - "Null Value Left"

  • ์„ค๋ช…: NVL์€ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด NULL์ผ ๋•Œ, ๋‘ ๋ฒˆ์งธ ๊ฐ’์„ ์™ผ์ชฝ(Left)์œผ๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • ์™ธ์šฐ๊ธฐ: "NVL์€ Null์„ Value๋กœ Left (์™ผ์ชฝ์œผ๋กœ ๋Œ์–ด์™€ ๋Œ€์ฒด)."
  • ๋น„์œ : ๋งŒ์•ฝ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด ๋น„์–ด ์žˆ์œผ๋ฉด, ์˜†์— ์žˆ๋Š” ๋‘ ๋ฒˆ์งธ ๊ฐ’์„ ๊ฐ€์ ธ๋‹ค ์ฑ„์›Œ ๋„ฃ๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜์„ธ์š”.

2. NVL2 - "Null Value, 2 Outcomes"

  • ์„ค๋ช…: NVL2๋Š” ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด NULL์ด๋ฉด ๋‘ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , NULL์ด ์•„๋‹ˆ๋ฉด ์„ธ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์™ธ์šฐ๊ธฐ: "NVL2๋Š” Null Value์—์„œ Left์™€ Right๋กœ 2๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜."
  • ๋น„์œ : ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด ์žˆ๋Š๋ƒ ์—†๋Š๋ƒ์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ธฐ์–ตํ•˜์„ธ์š”.

3. ISNULL - "Is It Null?"

  • ์„ค๋ช…: ISNULL์€ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด NULL์ธ์ง€ ํ™•์ธํ•˜๊ณ , ๊ทธ๋ ‡๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์™ธ์šฐ๊ธฐ: "ISNULL์€ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด NULL์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 'Is it null?'"
  • ๋น„์œ : "์ด๊ฒŒ NULL์ธ๊ฐ€?"๋ผ๊ณ  ๋ฌผ์–ด๋ณด๊ณ , ๊ทธ๋ ‡๋‹ค๋ฉด ๋Œ€์ฒด ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์„ธ์š”.

4. NULLIF - "Null If Equal"

  • ์„ค๋ช…: NULLIF๋Š” ๋‘ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์™ธ์šฐ๊ธฐ: "NULLIF๋Š” ๋‘ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 'Null If equal.'"
  • ๋น„์œ : "๋งŒ์•ฝ ๋‘ ๊ฐ’์ด ๊ฐ™๋‹ค(g=๊ฐ™๋‹ค)๋ฉด, ๊ฒฐ๊ณผ๋Š” NULL์ด ๋œ๋‹ค."๋ผ๊ณ  ๊ธฐ์–ตํ•˜์„ธ์š”.

5. COALESCE - "Collect Or Alternate"

  • ์„ค๋ช…: COALESCE๋Š” ์—ฌ๋Ÿฌ ๊ฐ’ ์ค‘์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ NULL์ด ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์™ธ์šฐ๊ธฐ: "COALESCE๋Š” ์—ฌ๋Ÿฌ ๊ฐ’ ์ค‘ ์ฒซ ๋ฒˆ์งธ NULL์ด ์•„๋‹Œ ๊ฐ’์„ ๊ณ ๋ฅธ๋‹ค. 'Collect or choose an alternative.'"
  • ๋น„์œ : "COALESCE๋Š” ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ ์ค‘์—์„œ ๊ฐ€์žฅ ๋จผ์ € ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”(์ฆ‰, NULL์ด ์•„๋‹Œ) ๊ฒƒ์„ ๊ณ ๋ฅธ๋‹ค."๋ผ๊ณ  ๊ธฐ์–ตํ•˜์„ธ์š”.

 

1. NVL (Oracle ์ „์šฉ)

NVL ํ•จ์ˆ˜๋Š” Oracle์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ NULL์ผ ๊ฒฝ์šฐ, ๋‘๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ: NVL(expr1, expr2)

SELECT NVL(Salary, 0) AS FinalSalary
FROM Employees;
  • Salary ์—ด์˜ ๊ฐ’์ด NULL ์ด๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์›๋ž˜ Salary ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, ์›”๊ธ‰์ด ์—†๋Š” ๊ฒฝ์šฐ(= NULL), 0์œผ๋กœ ๋Œ€์ฒด๋˜์–ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

2. NVL2 (Oracle ์ „์šฉ)

NVL2 ํ•จ์ˆ˜๋Š” Oracle์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ NULL์ด ์•„๋‹ˆ๋ฉด ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , NULL ์ด๋ฉด ์„ธ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ: NVL2(expr1, expr2, expr3)

SELECT NVL2(Commission, Commission, 0) AS FinalCommission
FROM Sales;
  • Commission ๊ฐ’์ด NULL ์ด ์•„๋‹ˆ๋ฉด ์›๋ž˜ Commission ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  NULL ์ด๋ฉด 0 ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, ์ปค๋ฏธ์…˜์ด NULL์ธ ๊ฒฝ์šฐ 0์œผ๋กœ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค.

 

3. ISNULL (SQL Server ์ „์šฉ)

ISNULL ํ•จ์ˆ˜๋Š” SQL Server์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ NULL์ผ ๊ฒฝ์šฐ, ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

Oracle์˜ NVL๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ: ISNULL(expr1, expr2)

SELECT ISNULL(Bonus, 500) AS FinalBonus
FROM Sales;
  • Bonus ๊ฐ’์ด NULL์ด๋ฉด 500์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์›๋ž˜ Bonus ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

4. NULLIF (๋ชจ๋“  DBMS ์ง€์›)

NULLIF ํ•จ์ˆ˜๋Š” ๋‘ ์ธ์ˆ˜๊ฐ€ ๋™์ผํ•˜๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ: NULLIF(expr1, expr2)

SELECT NULLIF(Salary, 0) AS AdjustedSalary
FROM Employees;
  • Salary ๊ฐ’์ด 0์ด๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์›๋ž˜์˜ Salary ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • Salary  ๊ฐ’์ด 0์ด๋ฉด NULL๋กœ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค

 

5. COALESCE (๋ชจ๋“  DBMS ์ง€์›)

COALESCE ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ์ธ์ˆ˜๋ฅผ ๋ฐ›์•„ ์ฒซ ๋ฒˆ์งธ๋กœ NULL์ด ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๋Œ€์ฒด ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์–ด ๋งค์šฐ ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ: COALESCE(expr1, expr2, ..., exprN)

SELECT COALESCE(Bonus, Commission, 0) AS FinalBonus
FROM Sales;
  • Bonus๊ฐ€ NULL์ด๋ฉด Commission์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , Commission๋„ NULL์ด๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ณด๋„ˆ์Šค์™€ ์ปค๋ฏธ์…˜์ด ๋ชจ๋‘ ์—†๋Š” ๊ฒฝ์šฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ 0์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•