DBAtlas · Atlas für Datenbankbetrieb

NULL-Behandlung (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;

Zur Behandlung von NULL (unbekannter Wert): COALESCE liefert den ersten Nicht-NULL-Wert der Liste (Standard, mehrere Argumente). Oracles NVL(a,b) ist die Entsprechung mit zwei Argumenten; NVL2 liefert je nachdem, ob der Wert NULL ist oder nicht, unterschiedliche Werte. NULLIF(a,b) liefert NULL, wenn beide gleich sind. Prüfe NULL immer mit IS NULL / IS NOT NULL (= NULL funktioniert nicht).

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 bietet Standard-COALESCE, NULLIF und IS NULL/IS NOT NULL; nutze COALESCE statt Oracles NVL. Für NULL-Vergleiche IS statt = verwenden; müssen zwei NULLs als gleich gelten, erledigt das der Operator '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 unterstützen Standard-COALESCE und NULLIF; IFNULL(a,b) ist eine Kurzform mit zwei Argumenten (ähnlich Oracle NVL). Der Operator <=> bietet NULL-sichere Gleichheit (behandelt zwei NULLs als gleich). NULL wird mit IS NULL / IS NOT NULL geprüft.

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 bietet Standard-COALESCE (mehrere Argumente, mit Datentyp-Promotion) und ISNULL(a,b) mit zwei Argumenten; beide unterscheiden sich in Feinheiten (COALESCE ist Standard und flexibler bei der Typrangfolge). Auch NULLIF und IS NULL werden unterstützt. ISNULL entspricht Oracle NVL (die Namen sind ähnlich).

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] } })

In der MongoDB-Aggregation entspricht der Operator $ifNull COALESCE (mehrere Argumente, liefert das erste, das nicht null/fehlend ist). In einer Abfrage trifft { field: null } sowohl Dokumente mit dem Wert null als auch Dokumente, denen das Feld ganz fehlt; mache die Unterscheidung mit { field: { $type: 'null' } } oder $exists explizit.

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 hat kein NULL im SQL-Sinn; die Entsprechung ist ein fehlender Schlüssel oder ein fehlendes Hash-Feld, und Befehle liefern in diesem Fall nil. Standardwertlogik (wie COALESCE) wird in der Anwendung mit einer nil-Prüfung umgesetzt. Die Existenz eines Felds wird mit HEXISTS geprüft.

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

In Elasticsearch werden null- oder fehlende Felder mit der 'exists'-Abfrage geprüft (must_not exists = Dokumente ohne das Feld). Für einen COALESCE-ähnlichen Standardwert definiere 'null_value' im Mapping oder weise fehlenden Einträgen in Aggregationen mit dem Parameter 'missing' einen Wert zu. Standardwerte lassen sich auch mit Runtime Fields/Skripten erzeugen.

In DBAtlas öffnen →