-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNull Value Replacement.sql
More file actions
85 lines (65 loc) · 3.33 KB
/
Null Value Replacement.sql
File metadata and controls
85 lines (65 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- Null Value Replacement
/*
In SQL, NULL values can be replaced or handled in various ways using built-in functions.
The three most common methods for replacing null values are using:
1. COALESCE() function
2. ISNULL()
3. CASE Statement
*/
-- 1. COALESCE() function:
/*
The COALESCE() function returns the first non-NULL value in the list of arguments.
It can be used to replace NULL values with a specified value.
*/
USE SQL_Server_DB
-- using Employee_Department table from SQL_Clauses_2 file.
SELECT * FROM Employee_Department
-- replace null value from Emp_Age column
SELECT Emp_Name, Emp_Age, COALESCE(Emp_Age, 0) AS Updated_Age FROM Employee_Department
-- replace null value from Emp_Salary column
SELECT Emp_Name, Emp_Salary, COALESCE(Emp_Salary, 0) AS Updated_Salary FROM Employee_Department
-- replace null value from Emp_Age column
SELECT Emp_Age, COALESCE(Emp_Age, (SELECT AVG(Emp_Age) FROM Employee_Department )) AS Updated_Age FROM Employee_Department
-- replace null value from Emp_Salary column
SELECT Emp_Salary, COALESCE(Emp_Salary, (SELECT AVG(Emp_Salary) FROM Employee_Department)) AS Updated_Salary FROM Employee_Department
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 2. ISNULL() function:
/*
SQL Server uses ISNULL(), for replacing null with specified value.
*/
SELECT Emp_name, Emp_Age, ISNULL(Emp_Age, 0) AS Updated_Age FROM Employee_Department
SELECT Emp_Name, Emp_Age, ISNULL(Emp_Age, (SELECT AVG(Emp_Age) FROM Employee_Department)) AS Updated_Age FROM Employee_Department
SELECT Emp_Age, ISNULL(Emp_Salary, (SELECT AVG(Emp_Age) FROM Employee_Department)) AS Updated_Age FROM Employee_Department
SELECT Emp_Salary, ISNULL(Emp_Salary, (SELECT AVG(Emp_Salary) FROM Employee_Department)) AS Updated_Salary FROM Employee_Department
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 3. CASE Statements:
-- You can also use a CASE statement to conditionally replace NULL values.
-- replace null value from Emp_Age column
SELECT Emp_Age,
CASE
WHEN Emp_Age IS NULL THEN 0
ELSE Emp_Age
END AS Emp_Age
FROM Employee_Department
-- replace null value from Emp_Salary column
SELECT Emp_Salary,
CASE
WHEN Emp_Salary IS NULL THEN 0
ELSE Emp_Salary
END AS Emp_Salary
FROM Employee_Department
-- replace null value from Emp_Age column
SELECT Emp_Age,
CASE
WHEN Emp_Age IS NULL THEN (SELECT AVG(Emp_Age) FROM Employee_Department)
ELSE Emp_Age
END AS Emp_Salary
FROM Employee_Department
-- replace null value from Emp_Salary column
SELECT Emp_Salary,
CASE
WHEN Emp_Salary IS NULL THEN (SELECT AVG(Emp_Salary) FROM Employee_Department)
ELSE Emp_Salary
END AS Empl_Salary
FROM Employee_Department
-- ♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦