Werken met NULL-waarden

Introductie tot Oracle SQL

Hadrien Lacroix

Content Developer

Wat zijn NULL-waarden?

  • Geen waarde
  • Niet hetzelfde als 0
  • Rekenkundige expressies met NULL worden NULL
    • NULL + 10 = NULL
  • Aggregaatfuncties negeren meestal NULL
    • COUNT telt geen NULL-waarden in een kolom
Introductie tot Oracle SQL

Waarom geven we om NULL-waarden?

Echte data is niet perfect.

  • Data opschonen
  • Ontbrekende data analyseren
Introductie tot Oracle SQL

Testen of een waarde NULL is

= kun je niet gebruiken om op NULL te testen

Gebruik in plaats daarvan:

  • IS NULL
    SELECT * FROM Customer WHERE LastName IS NULL
    
  • IS NOT NULL
    SELECT * FROM Customer WHERE LastName IS NOT NULL
    
Introductie tot Oracle SQL

NVL

NVL(x, y): zet x (kan NULL zijn) om naar y (niet-NULL).

SELECT NVL(HireDate, '11/19/2004')
FROM Employee
Introductie tot Oracle SQL

NULLIF

NULLIF(x, y): vergelijkt x en y, geeft terug

  • NULL als x=y
  • x als ze niet gelijk zijn
SELECT c.CustomerId, i.BillingCity, c.City, NULLIF(i.BillingCity, c.City) 
FROM Invoice i, Customer c
| CustomerId | BillingCity | City      | NULLIF |
|------------|-------------|-----------|--------|
| 48         | Oslo        | Amsterdam | Oslo   |
| 49         | Boston      | Vienne    | Boston |
| 59         | London      | London    | NULL   |
Introductie tot Oracle SQL

COALESCE

COALESCE: geeft de eerste niet-NULL waarde in een lijst terug

SELECT CustomerId, COALESCE(phone, email, fax) AS ContactMethod
FROM Customer
| CustomerId | ContactMethod           |
|------------|-------------------------|
| 59         | +91 080 22289999        |
| 58         | [email protected] |
| 57         | +56 (0)2 635 4444       |
Introductie tot Oracle SQL

Laten we oefenen!

Introductie tot Oracle SQL

Preparing Video For Download...