<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Denis Gobo : Nulls, SQL Server 2008</title><link>http://sqlblog.com/blogs/denis_gobo/archive/tags/Nulls/SQL+Server+2008/default.aspx</link><description>Tags: Nulls, SQL Server 2008</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Return Null If A Value Is A Certain Value</title><link>http://sqlblog.com/blogs/denis_gobo/archive/2007/11/06/3229.aspx</link><pubDate>Tue, 06 Nov 2007 18:28:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:3229</guid><dc:creator>Denis Gobo</dc:creator><slash:comments>7</slash:comments><comments>http://sqlblog.com/blogs/denis_gobo/comments/3229.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/denis_gobo/commentrss.aspx?PostID=3229</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/denis_gobo/rsscomments.aspx?PostID=3229</wfw:comment><description>&lt;P&gt;You need to return NULL only if the value of your data is a certain value. How do you do this? There are three different ways. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;NULLIF&lt;/STRONG&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;NULLIF&lt;/SPAN&gt;(@1&lt;SPAN style="COLOR:#ff0000;"&gt;,'D'&lt;/SPAN&gt;)&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;REPLACE&lt;/STRONG&gt;&lt;BR&gt;This should not really be used, I just added it here to demonstrate that you can in fact use it.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;REPLACE&lt;/SPAN&gt;(@1,&lt;SPAN style="COLOR:#ff0000;"&gt;'D'&lt;/SPAN&gt;,NULL)&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;STRONG&gt;CASE&lt;/STRONG&gt;&lt;BR&gt;With case you can test for a range of values. You can test for example for values between A and D. If you reverse the logic then you also don't need to provide the ELSE part since it defaults to NULL anyway.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; &lt;SPAN style="COLOR:#ff0000;"&gt;'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; NULL &lt;SPAN style="COLOR:#3333ff;"&gt;ELSE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#009900;"&gt;--No else needed&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; @1 &amp;lt;&amp;gt; &lt;SPAN style="COLOR:#ff0000;"&gt;'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;And this is how you test for a range.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#009900;"&gt;--Null&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; @1 BETWEEN &lt;SPAN style="COLOR:#ff0000;"&gt;'A'&lt;/SPAN&gt; AND&lt;SPAN style="COLOR:#ff0000;"&gt; 'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; NULL &lt;SPAN style="COLOR:#3333ff;"&gt;ELSE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#009900;"&gt;--E&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'E' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; @1 BETWEEN &lt;SPAN style="COLOR:#ff0000;"&gt;'A'&lt;/SPAN&gt; AND&lt;SPAN&gt; 'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; NULL &lt;SPAN style="COLOR:#3333ff;"&gt;ELSE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt; &lt;/P&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=3229" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/NULLIF/default.aspx">NULLIF</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Nulls/default.aspx">Nulls</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/sqL+Server+2000/default.aspx">sqL Server 2000</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/SQL+Server+2005/default.aspx">SQL Server 2005</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/SQL+Server+2008/default.aspx">SQL Server 2008</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Tip/default.aspx">Tip</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/trick/default.aspx">trick</category></item></channel></rss>