Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 February SQL Server: Find First NOT NULL Column using a COALESCE Function

SQL Server: Find First NOT NULL Column using a COALESCE Function

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

The COALESCE() is a one of my favourite function in SQL Server.
Using this function, we can check first NOT NULL column or field out of all following columns.
We can pass any number of parameters, and it returns the first NOT NULL value.

The COALESCE is ANSI standard function, and internally it is working like a CASE..WHEN expression.

For example:
We have three different PhoneNumber column for a single User and may be User has only one or two PhoneNumber, and now the requirement is to fetch at least one PhoneNumber from this three different PhoneNumber.

We can easily solve this kind of problem using COALESCE(), and it doesn’t need to write big CASE..WHEN expression.

Below is a small demonstration on this:

Create sample table and data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE tbl_UserPhoneNumbers
(
UserID INTEGER IDENTITY(1,1)
,UserName VARCHAR(50)
,PhoneNumber1 CHAR(10)
,PhoneNumber2 CHAR(10)
,PhoneNumber3 CHAR(10)
,CONSTRAINT pk_tbl_UserPhoneNumbers_UserID PRIMARY KEY(UserID)
)
GO
 
INSERT INTO tbl_UserPhoneNumbers
(UserName,PhoneNumber1,PhoneNumber2,PhoneNumber3)
VALUES
('Anvesh','9898245852',NULL ,NULL)
,('Roy',NULL,'8759654122',NULL)
,('Eric',NULL,NULL,'9565859812')
,('Lurin','9898652312','9685741252',NULL)
GO

SQL Server NULL Columns

You can find some NULL field in the above result.

Now use COALESCE() to SELECT first NOT NULL PhoneNumber:

1
2
3
4
5
SELECT
UserID
,UserName
,PhoneNumber=COALESCE(PhoneNumber1,PhoneNumber2,PhoneNumber3)
FROM tbl_UserPhoneNumbers

The Result:

SQL Server Coalesce Function

Feb 6, 2016Anvesh Patel
SQL Server: The TempDB is Full, Shrink it or Move itSQL Server 2012: Introduced LEAD function, the best alternative of Self Join to compare Next Row
Comments: 5
  1. norman
    February 7, 2016 at 6:42 pm

    Hey! This post couldn’t be written any better! Reading through this post reminds me of my old room mate!

    He always kept chatting about this. I will forward
    this page to him. Pretty sure he will have a
    good read. Thank you for sharing!

  2. Valen
    February 9, 2016 at 12:44 am

    I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!

  3. Denial
    February 14, 2016 at 12:21 am

    Enjoyed examining this, very good stuff, appreciate it.

  4. Robin
    February 24, 2016 at 3:38 am

    I like this web site very much, Its a real nice situation to read and get info . “If at first you don’t succeed, you’re running about average.” by M. H. Alderson.

  5. Monika
    May 6, 2016 at 8:22 pm

    You should take part in a contest for one of the best blogs on the web. I will recommend this site!

Anvesh Patel
Anvesh Patel

Database Engineer

February 6, 2016 SQL ServerAnvesh Patel, COALESCE, database, database research and development, dbrnd, NOT NULL, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....