SQL Day 16: COALESCE() Function

LO1: COALESCE() function with example

Prerequisite: Replacing NULL in SQL

COALESCE function return the first NON NULL value.
Let’s say we have a table called tblPersons which contains data as below.



In this table, some persons have the first name, some have the middle name, some have the last name or some have the all the three values.

So the COALESCE function will fetch the first NON NULL values from the table i.e. this function will look into the each column value if it finds NULL then it will move to the next column to get the value.

This process will continue until all the columns in the select list are traversed.

select Id, COALESCE(FirstName, MiddleName, LastName) as Name
from tblPersons


If all the columns contain the NULL value then that row will be included in the result.

That’s all for today. Thank You.

Comments

Popular posts from this blog

SQL Day 11: Group By in SQL Server

JAVA Script Executor Code to Click on an Element

SQL Day 2: Creating, Altering and Dropping a Database