Click here to Skip to main content
Click here to Skip to main content

Tagged as

Using Coalesce() in sqlserver

, 11 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
SQL Server built-in functions Coalesce()

Introduction

When we have multi-value attribute with single or more null values in a Table, the Coalesce() function is very useful.

Using the Code

If you consider the below facts placed in a employee table with Id, Name, ph_no, Alt_no, Office no.

id

Name

Ph_ no

Alt_ no

Office no

101

Albert

999999

456453

321333

102

khan

null

null

123455

103

victor

112121

null

null

104

lovely

null

null

1897321

The above Employee table may have single value or three values. If it has single value, then it fills null values with remaining attributes.

When we retrieve the number from employee table, that number Should Not be Null value. To get not null value from employee table, we use Coalesce() function. It returns the first encountered Not Null Value from employee table.

select id , name ,coalesce(Ph_no,Alt_no,Office_no) as contact number from employee 

It returns:

id

Name

Contactnumber

101

Albert

999999

102

khan

123455

103

victor

112121

104

lovely

1897321

Thank you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

blnreddy
Web Developer trainer
India India
iam freelancer in software in dotnet....
now iam engaged with wipro through TSE at hyderabad
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAnimesh Datta29-May-14 20:29 
Nice one
GeneralMy vote of 5 Pinprofessionalashumeerut12-May-14 8:05 
GeneralMy vote of 5 Pinmembershravanyadav18-Mar-14 4:09 
QuestionOne doubt Pinprofessional♥…ЯҠ…♥23-Dec-13 18:52 
AnswerRe: One doubt Pinmemberblnreddy19-Mar-14 9:38 
GeneralRe: One doubt Pinprofessional♥…ЯҠ…♥19-Mar-14 19:24 
GeneralMy vote of 5 PinmemberBalasubramanian T4-Aug-13 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 11 Jun 2013
Article Copyright 2013 by blnreddy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid