1. Back To Blog

SQL Server interview question - What is the use coalesce in SQL Server?

This is  simple SQL Server interview question  and answer is simple.It's just that many developers have not heard it so it some time becomes confusing the answer same.

Coalesce return the first non-null column from more than one columns. For instance you can see in the below table either "First Name" has null values "Surname" as null value. Now you would like to pull values using coalesce function and it will return the column which will have null values.



So if you fire the below SQL statement with coalesce.


SELECT   coalesce(FirstName, SurName) as Name

FROM         tblPerson


So when:-

  •  If "Surname" is null and "FirstName" is not null,then "FirstName" is returned.
  •  If "FirstName" is null and "SurName" is not null,then "SurName" will be returned.

Below is the output/

 




 

Here's simple list of Six important SQL Server interview question with answers,do have look at the same.

This question is taken from Chapter 3 SQL chapter  from the 300 pages book SQL Server interview questions and answers. You can also see our blog which is exclusive for SQL Server interviews.

You can also see our blog which is exclusive for SQL Server interviews.

Do watch below informative video for SQL Server Interview Question and Answers:-

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social