Different ways to replace NULL in sql server - Part 15
In this video session, we will learn about different ways to replace NULL values in SQL Server. Please watch Part 14, before continuing.
Consider the Employees table below.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg82O1bWN_t6QoYR_bnL1g4kYVUnI0UVj59HfxwMlRWj8wR_GvegNTZ0eHnQzYIBuvppiWDrbvyexsUTjX16G2E2pVb2P-6nHJvIyk2n23m77V7ilW2YlsIyKCfB4TmtprlfbX2_7j2Qd4r/s1600/tblEmployee.png)
In Part 14, we have learnt writing a LEFT OUTER SELF JOIN query, which produced the following output.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0EjPDp-r0Z0eOU5Ae-pkeiLgN_Jn6T-c5WG7j3AQYGg5Y9fJgANhVY72GOpMghbMQn0yP7B4qg3yFujgkX28MfZlSXYyOr_brRFSczaXcVWw8ekdZuJJXBqS5SedqmR_vsx79IbMAVcSH/s1600/Self+Join.png)
In the output, MANAGER column, for Todd's rows is NULL. I want to replace the NULLvalue, with 'No Manager'
Replacing NULL value using ISNULL() function: We are passing 2 parameters to IsNULL() function. If M.Name returns NULL, then 'No Manager' string is used as the replacement value.
SELECT E.Name as Employee, ISNULL(M.Name,'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
Replacing NULL value using CASE Statement:
SELECT E.Name as Employee, CASE WHEN M.Name IS NULL THEN 'No Manager'
ELSE M.Name END as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
Replacing NULL value using COALESCE() function: COALESCE() function, returns the first NON NULL value.
SELECT E.Name as Employee, COALESCE(M.Name, 'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
We will discuss about COALESCE() function in detail, in the next session
Consider the Employees table below.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg82O1bWN_t6QoYR_bnL1g4kYVUnI0UVj59HfxwMlRWj8wR_GvegNTZ0eHnQzYIBuvppiWDrbvyexsUTjX16G2E2pVb2P-6nHJvIyk2n23m77V7ilW2YlsIyKCfB4TmtprlfbX2_7j2Qd4r/s1600/tblEmployee.png)
In Part 14, we have learnt writing a LEFT OUTER SELF JOIN query, which produced the following output.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0EjPDp-r0Z0eOU5Ae-pkeiLgN_Jn6T-c5WG7j3AQYGg5Y9fJgANhVY72GOpMghbMQn0yP7B4qg3yFujgkX28MfZlSXYyOr_brRFSczaXcVWw8ekdZuJJXBqS5SedqmR_vsx79IbMAVcSH/s1600/Self+Join.png)
In the output, MANAGER column, for Todd's rows is NULL. I want to replace the NULLvalue, with 'No Manager'
Replacing NULL value using ISNULL() function: We are passing 2 parameters to IsNULL() function. If M.Name returns NULL, then 'No Manager' string is used as the replacement value.
SELECT E.Name as Employee, ISNULL(M.Name,'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
Replacing NULL value using CASE Statement:
SELECT E.Name as Employee, CASE WHEN M.Name IS NULL THEN 'No Manager'
ELSE M.Name END as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
Replacing NULL value using COALESCE() function: COALESCE() function, returns the first NON NULL value.
SELECT E.Name as Employee, COALESCE(M.Name, 'No Manager') as Manager
FROM tblEmployee E
LEFT JOIN tblEmployee M
ON E.ManagerID = M.EmployeeID
We will discuss about COALESCE() function in detail, in the next session
No comments:
Post a Comment