SQL Server intermédiaire
Ginger Grant
Instructor
Quand il n'y a pas de donnée, le champ de base de données contient NULL
Comme NULL n'est pas un nombre, on ne peut pas utiliser =, < ou > pour trouver ou comparer des valeurs manquantes
Pour savoir si une colonne contient NULL, utilisez IS NULL et IS NOT NULL
SELECT Country, InternetUse, Year
FROM EconomicIndicators
WHERE InternetUse IS NOT NULL
+-------------------+-------------------+------------+
|Country |InternetUse |Year |
|-------------------+-------------------+------------+
|Afghanistan |4.58066992 |2011 |
|Albania |49 |2011 |
|Algeria |14 |2011 |
....
+-------------------+-------------------+------------+
SELECT Country, InternetUse, Year
FROM EconomicIndicators
WHERE InternetUse IS NULL
+-------------------+-------------------+------------+
|Country |InternetUse |Year |
|-------------------+-------------------+------------+
|Angola |NULL |2013 |
|Argentina |NULL |2013 |
|Armenia |NULL |2013 |
....
+-------------------+-------------------+------------+
'' permet de repérer les videsLEN > 0SELECT Country, GDP, Year
FROM EconomicIndicators
WHERE LEN(GDP) > 0
+-------------------+-------------------+------------+
|Country |GDP |Year |
|-------------------+-------------------+------------+
|Afghanistan |54852215624 |2011 |
|Albania |29334492905 |2011 |
|Algeria |453558093404 |2011 |
....
+-------------------+-------------------+------------+
SELECT GDP, Country,
ISNULL(Country, 'Unknown') AS NewCountry
FROM EconomicIndicators
+-------------------+----------------+----------------+
|GDP |Country |NewCountry |
|-------------------+----------------+----------------+
|5867920022 |NULL |Unknown |
|597873038497 |South Africa |South Africa |
|1474091271101 |NULL |Unknown |
...
+-------------------+----------------+----------------+
/*Substituting values from one column for another with ISNULL*/
SELECT TradeGDPPercent, ImportGoodPercent,
ISNULL(TradeGDPPercent, ImportGoodPercent) AS NewPercent
FROM EconomicIndicators
+-------------------+------------------+----------------+
|TradeGDPPercent |ImportGoodPercent |NewPercent |
|-------------------+------------------+----------------+
|NULL |56.7 |56.7 |
|52.18720739 |51.75273421 |52.18720739 |
|NULL |NULL |NULL |
...
+-------------------+------------------+----------------+
COALESCE retourne la première valeur non manquante
COALESCE( value_1, value_2, value_3, ... value_n )
value_1 est NULL et value_2 ne l'est pas, retourne value_2value_1 et value_2 sont NULL et que value_3 ne l'est pas, retourne value_3SELECT TradeGDPPercent, ImportGoodPercent,
COALESCE(TradeGDPPercent, ImportGoodPercent, 'N/A') AS NewPercent
FROM EconomicIndicators
+-------------------+--------------------+---------------+
|TradeGDPPercent |ImportGoodPercent |NewPercent |
|-------------------+--------------------+---------------+
|NULL |56.7 |56.7 |
|NULL |NULL |N/A |
|52.18720739 |51.75273421 |52.18720739 |
+-------------------+--------------------+---------------+
SQL Server intermédiaire