DBAtlas · 데이터베이스 운영 아틀라스

NULL 처리(COALESCE/NVL/NULLIF/IS NULL)

Oracle

-- COALESCE: returns the first non-NULL value (standard)
SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;

-- Oracle-specific NVL / NVL2
SELECT NVL(balance, 0) FROM customer;
SELECT NVL2(discount, 'yes', 'no') FROM orders;

-- NULLIF and IS NULL
SELECT NULLIF(a, b) FROM t;   -- NULL if a=b
SELECT name FROM customer WHERE phone IS NULL;

NULL(알 수 없는 값) 처리: COALESCE는 목록에서 NULL이 아닌 첫 값을 반환한다(표준, 다중 인수). Oracle의 NVL(a,b)은 인수 2개짜리 대응 함수이며, NVL2는 값이 NULL인지 여부에 따라 다른 값을 반환한다. NULLIF(a,b)는 두 값이 같으면 NULL을 반환한다. NULL은 항상 IS NULL / IS NOT NULL로 검사한다(= NULL은 동작하지 않는다).

PostgreSQL

SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;
SELECT NULLIF(divisor, 0) FROM t;   -- prevent division by zero
SELECT name FROM customer WHERE phone IS NULL;
-- The equivalent of Oracle NVL is COALESCE

PostgreSQL은 표준 COALESCE, NULLIF, IS NULL/IS NOT NULL을 제공하며, Oracle의 NVL 대신 COALESCE를 사용한다. NULL 비교에는 =가 아닌 IS를 사용한다. 두 NULL을 같은 값으로 취급해야 하면 'IS NOT DISTINCT FROM' 연산자를 사용한다.

MySQL

SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;

-- MySQL shortcut: IFNULL (two arguments)
SELECT IFNULL(balance, 0) FROM customer;
SELECT NULLIF(a, b) FROM t;
SELECT name FROM customer WHERE phone IS NULL;

MySQL/MariaDB는 표준 COALESCE와 NULLIF를 지원하며, IFNULL(a,b)은 인수 2개짜리 약식이다(Oracle NVL과 유사). <=> 연산자는 NULL 안전 동등 비교를 제공한다(두 NULL을 같은 값으로 취급). NULL은 IS NULL / IS NOT NULL로 검사한다.

SQL Server

SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;

-- SQL Server shortcut: ISNULL (two arguments)
SELECT ISNULL(balance, 0) FROM customer;
SELECT NULLIF(a, b) FROM t;
SELECT name FROM customer WHERE phone IS NULL;

SQL Server는 표준 COALESCE(다중 인수, 데이터 타입 승격)와 인수 2개짜리 ISNULL(a,b)을 제공하며, 둘은 미묘하게 다르다(COALESCE는 표준이며 타입 우선순위 면에서 더 유연하다). NULLIF와 IS NULL도 지원한다. ISNULL은 Oracle NVL에 해당한다(이름이 비슷하다).

MongoDB

// COALESCE equivalent: $ifNull
db.customer.aggregate([
  { $project: { name: 1,
      contact: { $ifNull: ["$phone", "$mobile", "none"] } } }
])
// NULL / missing field check:
db.customer.find({ phone: { $in: [null] } })

MongoDB aggregation에서 COALESCE에 해당하는 것은 $ifNull 연산자다(다중 인수, null/missing이 아닌 첫 값 반환). 쿼리에서 { field: null }은 값이 null인 문서와 필드가 아예 없는 문서를 모두 매칭하므로, { field: { $type: 'null' } }이나 $exists로 구분을 명확히 한다.

Redis

# Instead of NULL, Redis has the concept of 'no such key/field':
EXISTS customer:1              # does the key exist?
HGET customer:1 phone        # returns nil if the field is absent
GET missing_key                  # (nil)

Redis에는 SQL 의미의 NULL이 없으며, 키나 Hash 필드가 없는 것이 이에 해당하고 이때 명령은 nil을 반환한다. 기본값 로직(COALESCE 같은)은 애플리케이션 측에서 nil 검사로 적용한다. 필드 존재는 HEXISTS로 확인한다.

Elasticsearch

# Default assignment when a field is missing (missing bucket / null_value)
# Find documents without the field in a query:
GET customer/_search
{ "query": { "bool": { "must_not": { "exists": { "field": "phone" } } } } }
# Map empty values to a specific value with null_value in the mapping

Elasticsearch에서 null이거나 없는 필드는 'exists' 쿼리로 확인한다(must_not exists = 필드가 없는 문서). COALESCE와 같은 기본값이 필요하면 mapping에 'null_value'를 정의하거나, aggregation에서 'missing' 파라미터로 누락된 항목에 값을 지정한다. 기본값은 runtime field/스크립트로도 생성할 수 있다.

DBAtlas에서 열기 →