<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Explanation and Comparison of NULLIF and ISNULL</title>
	<atom:link href="http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: mattmc3</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-50733</link>
		<dc:creator>mattmc3</dc:creator>
		<pubDate>Thu, 09 Apr 2009 20:26:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-50733</guid>
		<description>Now, here&#039;s a fun one... if you don&#039;t want to set ansi_nulls, you can use the isnull()/nullif() combo when comparing two values in a table.

The trick is in this line: isnull(nullif(val1, val2), nullif(val2, val1)) is null

--------------------------------------
--------------------------------------
if object_id(&#039;tempdb..#test&#039;) is not null drop table #test
create table #test (
	val1 varchar(50),
	val2 varchar(50)
)

insert into #test
select &#039;Bill&#039;, &#039;Bill&#039;
union all
select null, null
union all
select &#039;Bill&#039;, null
union all
select &#039;Bill&#039;, &#039;Gates&#039;
union all
select null, &#039;Gates&#039;

select
	val1,
	val2,
	case
		when val1 = val2 then &#039;(wrong) equal&#039;
		else &#039;&#039;
	end as WrongEquals,
	case
		when (val1  val2) then &#039;(wrong) NOT equal&#039;
		else &#039;&#039;
	end as WrongNotEquals,
	NULLIF(val1, val2) as NullIf1_2,
	NULLIF(val2, val1) as NullIf2_1,
	case
		when isnull(nullif(val1, val2), nullif(val2, val1)) is null then &#039;eqivalent!&#039;
		else &#039;&#039;
	end as OddlyCorrect_Equivalent,
	case
		when isnull(nullif(val1, val2), nullif(val2, val1)) is not null then &#039;NOT eqivalent!&#039;
		else &#039;&#039;
	end as OddlyCorrect_NotEquivalent
from
	#test
--------------------------------------
--------------------------------------

Enjoy wrapping your mind around that.</description>
		<content:encoded><![CDATA[<p>Now, here&#8217;s a fun one&#8230; if you don&#8217;t want to set ansi_nulls, you can use the isnull()/nullif() combo when comparing two values in a table.</p>
<p>The trick is in this line: isnull(nullif(val1, val2), nullif(val2, val1)) is null</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
if object_id(&#8216;tempdb..#test&#8217;) is not null drop table #test<br />
create table #test (<br />
	val1 varchar(50),<br />
	val2 varchar(50)<br />
)</p>
<p>insert into #test<br />
select &#8216;Bill&#8217;, &#8216;Bill&#8217;<br />
union all<br />
select null, null<br />
union all<br />
select &#8216;Bill&#8217;, null<br />
union all<br />
select &#8216;Bill&#8217;, &#8216;Gates&#8217;<br />
union all<br />
select null, &#8216;Gates&#8217;</p>
<p>select<br />
	val1,<br />
	val2,<br />
	case<br />
		when val1 = val2 then &#8216;(wrong) equal&#8217;<br />
		else &#8221;<br />
	end as WrongEquals,<br />
	case<br />
		when (val1  val2) then &#8216;(wrong) NOT equal&#8217;<br />
		else &#8221;<br />
	end as WrongNotEquals,<br />
	NULLIF(val1, val2) as NullIf1_2,<br />
	NULLIF(val2, val1) as NullIf2_1,<br />
	case<br />
		when isnull(nullif(val1, val2), nullif(val2, val1)) is null then &#8216;eqivalent!&#8217;<br />
		else &#8221;<br />
	end as OddlyCorrect_Equivalent,<br />
	case<br />
		when isnull(nullif(val1, val2), nullif(val2, val1)) is not null then &#8216;NOT eqivalent!&#8217;<br />
		else &#8221;<br />
	end as OddlyCorrect_NotEquivalent<br />
from<br />
	#test<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Enjoy wrapping your mind around that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-47223</link>
		<dc:creator>SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Tue, 24 Feb 2009 12:09:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-47223</guid>
		<description>[...] SQL SERVER - Explanation and Comparison of NULLIF and ISNULL [...]</description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER &#8211; Explanation and Comparison of NULLIF and ISNULL [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neal Patel</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-37322</link>
		<dc:creator>Neal Patel</dc:creator>
		<pubDate>Thu, 08 May 2008 17:42:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-37322</guid>
		<description>I have a serious of bit values.  I want to display column if bit value is 1 and hide column if bit value is 0.  How can I achieve this.  Here is what I have so far.

ALTER         PROCEDURE [dbo].[sp_datadump_selectfromcheckbox] 
	@project_ID	int,
 	@Title_isChecked bit 
	
	
	

AS
BEGIN 
SELECT  PRSDATA.Anomaly_ID,

CASE WHEN @Title_isChecked =&#039;1&#039; THEN (Select PRSDATA.Title) END &#039;  &#039;


	

	from PRSDATA,PRSDATA1, PRSDATA2, PRSDATA3, PRSDATA4,  PRSDATA5, PRSDATA6, PRSDATA7 , PRSDATA8 
	WHERE PRSDATA.anomaly_id = PRSDATA1.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA2.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA3.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA4.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA5.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA6.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA7.anomaly_id and 
	PRSDATA.anomaly_id = PRSDATA8.anomaly_id and 
	PRSDATA.Main_Project_Affected =   @project_ID
  
  
	
END	
	if (@@error = 0)
		begin
			RETURN 0
		end
	else
		begin
			RETURN 1
		end</description>
		<content:encoded><![CDATA[<p>I have a serious of bit values.  I want to display column if bit value is 1 and hide column if bit value is 0.  How can I achieve this.  Here is what I have so far.</p>
<p>ALTER         PROCEDURE [dbo].[sp_datadump_selectfromcheckbox]<br />
	@project_ID	int,<br />
 	@Title_isChecked bit </p>
<p>AS<br />
BEGIN<br />
SELECT  PRSDATA.Anomaly_ID,</p>
<p>CASE WHEN @Title_isChecked =&#8217;1&#8242; THEN (Select PRSDATA.Title) END &#8216;  &#8216;</p>
<p>	from PRSDATA,PRSDATA1, PRSDATA2, PRSDATA3, PRSDATA4,  PRSDATA5, PRSDATA6, PRSDATA7 , PRSDATA8<br />
	WHERE PRSDATA.anomaly_id = PRSDATA1.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA2.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA3.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA4.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA5.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA6.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA7.anomaly_id and<br />
	PRSDATA.anomaly_id = PRSDATA8.anomaly_id and<br />
	PRSDATA.Main_Project_Affected =   @project_ID</p>
<p>END<br />
	if (@@error = 0)<br />
		begin<br />
			RETURN 0<br />
		end<br />
	else<br />
		begin<br />
			RETURN 1<br />
		end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aris Routis</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-16990</link>
		<dc:creator>Aris Routis</dc:creator>
		<pubDate>Thu, 01 Nov 2007 11:48:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-16990</guid>
		<description>declare @Test tinyint
set @Test = NULL
/*
Change to
set @Test = 0 or
set @Test = 1
keeping every other SET line remarked to check different
behaviours.
*/

select Case IsNull(@Test, 2) —
when 2 Then ‘IS NULL’
else ‘NOT NULL’
End Result

That should do it.</description>
		<content:encoded><![CDATA[<p>declare @Test tinyint<br />
set @Test = NULL<br />
/*<br />
Change to<br />
set @Test = 0 or<br />
set @Test = 1<br />
keeping every other SET line remarked to check different<br />
behaviours.<br />
*/</p>
<p>select Case IsNull(@Test, 2) —<br />
when 2 Then ‘IS NULL’<br />
else ‘NOT NULL’<br />
End Result</p>
<p>That should do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aris Routis</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-16988</link>
		<dc:creator>Aris Routis</dc:creator>
		<pubDate>Thu, 01 Nov 2007 11:44:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-16988</guid>
		<description>Yasar,
You should rephrase this to something like this 

declare @Test bit
set @Test = NULL
/*
Change to 
set @Test = 0 or
set @Test = 1
keeping every other SET line remarked to check different
behaviours.
*/

select Case IsNull(@Test, 0) -- 
	when 0 Then &#039;IS NULL&#039;
	else &#039;NOT NULL&#039;
End Result

That will work just fine.</description>
		<content:encoded><![CDATA[<p>Yasar,<br />
You should rephrase this to something like this </p>
<p>declare @Test bit<br />
set @Test = NULL<br />
/*<br />
Change to<br />
set @Test = 0 or<br />
set @Test = 1<br />
keeping every other SET line remarked to check different<br />
behaviours.<br />
*/</p>
<p>select Case IsNull(@Test, 0) &#8212;<br />
	when 0 Then &#8216;IS NULL&#8217;<br />
	else &#8216;NOT NULL&#8217;<br />
End Result</p>
<p>That will work just fine.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yasar</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-12808</link>
		<dc:creator>Yasar</dc:creator>
		<pubDate>Sun, 16 Sep 2007 16:33:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-12808</guid>
		<description>I have the following code that uses the IsNull function.


declare @Test bit;
set @Test = &#039;false&#039;;

select (	Case When IsNull(@Test, &#039;&#039;) = &#039;&#039;
			Then &#039;IS NULL&#039; 
			Else &#039;NOT NULL&#039;
			End);

I would expect &#039;NOT NULL&#039; to be returned as @Test is set to a value &#039;false&#039;. However &#039;IS NULL&#039; is output instead.

Does SQL Server treat NULL the same as &#039;false&#039;?</description>
		<content:encoded><![CDATA[<p>I have the following code that uses the IsNull function.</p>
<p>declare @Test bit;<br />
set @Test = &#8216;false&#8217;;</p>
<p>select (	Case When IsNull(@Test, &#8221;) = &#8221;<br />
			Then &#8216;IS NULL&#8217;<br />
			Else &#8216;NOT NULL&#8217;<br />
			End);</p>
<p>I would expect &#8216;NOT NULL&#8217; to be returned as @Test is set to a value &#8216;false&#8217;. However &#8216;IS NULL&#8217; is output instead.</p>
<p>Does SQL Server treat NULL the same as &#8216;false&#8217;?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-3913</link>
		<dc:creator>pinaldave</dc:creator>
		<pubDate>Fri, 22 Jun 2007 21:21:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-3913</guid>
		<description>Hello The Waterproof SQL Champ good work,

I was expecting it to be done without using WHILE but I did not make that clear in question so it works.
Good Work. I modified your code to show how the infinite code can be generated. However following script will run till 10 iteration and will stop.

SET ANSI_NULLS OFF

DECLARE @test AS INT
SET @test = 1

WHILE ISNULL( NULLIF(@test, @test), @test ) = @test
BEGIN
PRINT &#039;HI&#039;
SET @test = @test + 1
IF @test &gt; 10
BREAK
END
GO

Warning : Do not run this code on production machine. Make sure you test it. The reason it is published here, so user who have similar problem can prevent infinite loop from happening.</description>
		<content:encoded><![CDATA[<p>Hello The Waterproof SQL Champ good work,</p>
<p>I was expecting it to be done without using WHILE but I did not make that clear in question so it works.<br />
Good Work. I modified your code to show how the infinite code can be generated. However following script will run till 10 iteration and will stop.</p>
<p>SET ANSI_NULLS OFF</p>
<p>DECLARE @test AS INT<br />
SET @test = 1</p>
<p>WHILE ISNULL( NULLIF(@test, @test), @test ) = @test<br />
BEGIN<br />
PRINT &#8216;HI&#8217;<br />
SET @test = @test + 1<br />
IF @test &gt; 10<br />
BREAK<br />
END<br />
GO</p>
<p>Warning : Do not run this code on production machine. Make sure you test it. The reason it is published here, so user who have similar problem can prevent infinite loop from happening.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The Waterproof SQL Champ</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-3912</link>
		<dc:creator>The Waterproof SQL Champ</dc:creator>
		<pubDate>Fri, 22 Jun 2007 21:07:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-3912</guid>
		<description>SET ANSI_NULLS OFF

DECLARE @test AS INT
SET @test = 1

WHILE ISNULL( NULLIF(@test, @test), @test ) = @test
BEGIN
	PRINT &#039;HI&#039;
	BREAK
END

Making @test = NULL would still make it print &#039;HI&#039;</description>
		<content:encoded><![CDATA[<p>SET ANSI_NULLS OFF</p>
<p>DECLARE @test AS INT<br />
SET @test = 1</p>
<p>WHILE ISNULL( NULLIF(@test, @test), @test ) = @test<br />
BEGIN<br />
	PRINT &#8216;HI&#8217;<br />
	BREAK<br />
END</p>
<p>Making @test = NULL would still make it print &#8216;HI&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Angelos</title>
		<link>http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-3910</link>
		<dc:creator>Angelos</dc:creator>
		<pubDate>Fri, 22 Jun 2007 16:39:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/22/sql-server-explanation-and-comparison-of-nullif-and-isnull/#comment-3910</guid>
		<description>Thanks, Nice tip

Maybe this ?

IsNull(null, nullif(1, 1)), but I guess it would just return null :P</description>
		<content:encoded><![CDATA[<p>Thanks, Nice tip</p>
<p>Maybe this ?</p>
<p>IsNull(null, nullif(1, 1)), but I guess it would just return null :P</p>
]]></content:encoded>
	</item>
</channel>
</rss>
