<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>COALESCE &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/coalesce/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 29 Sep 2017 11:48:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQL Server: Find First NOT NULL Column using a COALESCE Function</title>
		<link>https://www.dbrnd.com/2016/02/sql-server-find-first-not-null-column-using-a-coalesce-function/</link>
					<comments>https://www.dbrnd.com/2016/02/sql-server-find-first-not-null-column-using-a-coalesce-function/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 06 Feb 2016 20:58:54 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[COALESCE]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[NOT NULL]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1478</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&amp;linkname=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&amp;linkname=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&amp;linkname=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&amp;linkname=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&amp;linkname=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&amp;linkname=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F02%2Fsql-server-find-first-not-null-column-using-a-coalesce-function%2F&#038;title=SQL%20Server%3A%20Find%20First%20NOT%20NULL%20Column%20using%20a%20COALESCE%20Function" data-a2a-url="https://www.dbrnd.com/2016/02/sql-server-find-first-not-null-column-using-a-coalesce-function/" data-a2a-title="SQL Server: Find First NOT NULL Column using a COALESCE Function"></a></p><p>The COALESCE() is a one of my favourite function in SQL Server.<br />
Using this function, we can check first NOT NULL column or field out of all following columns.<br />
We can pass any number of parameters, and it returns the first NOT NULL value.</p>
<blockquote><p>The COALESCE is ANSI standard function, and internally it is working like a CASE..WHEN expression.</p></blockquote>
<p><strong>For example:</strong><br />
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.</p>
<p>We can easily solve this kind of problem using COALESCE(), and it doesn&#8217;t need to write big CASE..WHEN expression.</p>
<p>Below is a small demonstration on this:</p>
<p><strong>Create sample table and data:</strong></p><pre class="crayon-plain-tag">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</pre><p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerNULLColumns.png" rel="attachment wp-att-1484"><img decoding="async" class="alignnone wp-image-1484 size-full" title="SQL Server NULL Columns" src="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerNULLColumns.png" alt="SQL Server NULL Columns" width="402" height="102" srcset="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerNULLColumns.png 402w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerNULLColumns-300x76.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerNULLColumns-150x38.png 150w" sizes="(max-width: 402px) 100vw, 402px" /></a></p>
<p>You can find some NULL field in the above result.</p>
<p><strong>Now use COALESCE() to SELECT first NOT NULL PhoneNumber:</strong></p><pre class="crayon-plain-tag">SELECT
	UserID
	,UserName
	,PhoneNumber=COALESCE(PhoneNumber1,PhoneNumber2,PhoneNumber3)
FROM tbl_UserPhoneNumbers</pre><p>
<strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerCoalesce.png" rel="attachment wp-att-1485"><img decoding="async" class="alignnone wp-image-1485 size-full" title="SQL Server Coalesce Function" src="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerCoalesce.png" alt="SQL Server Coalesce Function" width="217" height="107" srcset="https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerCoalesce.png 217w, https://www.dbrnd.com/wp-content/uploads/2016/02/SQLServerCoalesce-150x74.png 150w" sizes="(max-width: 217px) 100vw, 217px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/02/sql-server-find-first-not-null-column-using-a-coalesce-function/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
