﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Bhuvnesh  / Play with NULLIF / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 18 Jun 2013 00:33:52 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Awesome question. didn't know nullIf can be these much tricky...</description><pubDate>Sun, 07 Nov 2010 16:46:58 GMT</pubDate><dc:creator>Dhruvesh Shah</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>When you cast(blank('') as int), it returns 0. so when you compare 0 with 0 nullif returns NULL.declare @a smallintset @a= 0select nullif(@a,0)Ans: NULL</description><pubDate>Tue, 22 Jun 2010 04:18:33 GMT</pubDate><dc:creator>psharma-1140604</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Thanks for the info Hugo.  I can now see where it would be useful in a couple of queries I have for some reports - where I was getting a potential division by zero, depending on the input data.  I seem to remember putting a rather inelegant solution (in comparison) to handle it.  Will have to revisit.</description><pubDate>Thu, 06 May 2010 14:46:42 GMT</pubDate><dc:creator>thecosmictrickster@gmail.com</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Great question. I learned that I should read more carefully. This question was about NULLIF (which I had never learned), not ISNULL! :blush:Thanks,webrunner</description><pubDate>Tue, 04 May 2010 07:36:33 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Paul White NZ (5/3/2010)[/b][hr]@sknox:I laughed when I saw Hugo's :Whistling: reply.Paul[/quote]So did I ... AFTER I'd already posted my reply. Oh, the wonders of asynchronous operations!</description><pubDate>Tue, 04 May 2010 07:24:39 GMT</pubDate><dc:creator>sknox</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Adam Haines (5/3/2010)[/b][hr]I think it is also important to understand what is going on underneath the hood, when NULLIF is used. Unbeknownst to some, NULLIF is actually a case expression under the hood, which means it is subject to data type precedence.  The second value is not always implicitly converted to the data type of the first value.  Whichever side has the less data type precedence will be converted to the other data type.  This can cause implict conversion errors, if the columns cannot be converted to the higher data type. e.g.[code]DECLARE @t TABLE(fl int);INSERT INTO @t VALUES (0);DECLARE @var CHAR(1),	@var2 DATETIMESET @var = ''SET @var2 = GETDATE()select nullif(fl, @var)FROM @t--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN [fl]=CONVERT_IMPLICIT(int,[@var],0) THEN NULL ELSE [fl] END))select nullif(fl, @var2) FROM @t--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN CONVERT_IMPLICIT(datetime,[fl],0)=[@var2] THEN NULL ELSE [fl] END))[/code][/quote] thats called having red meat with cold beer on beach....PERFECT fruit for this thread.</description><pubDate>Tue, 04 May 2010 00:03:40 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>@sknox:I'm pretty sure Hugo posted the NULLIF 'solution' as a bit of fun - though the idea behind it is very clever and worth posting just for that.My comment was intended to be humourous as much as anything else - I laughed when I saw Hugo's :Whistling: reply.(I don't disagree with anything you wrote by the way)Paul</description><pubDate>Mon, 03 May 2010 19:58:16 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Interesting question - thanks.The explanation led me to wonder what other character CAST() would return with 0.  I've found two:select cast('+' as smallint)select cast('-' as smallint)But why should a negative or positive sign return a zero?</description><pubDate>Mon, 03 May 2010 14:30:46 GMT</pubDate><dc:creator>mtillman-921105</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>I think it is also important to understand what is going on underneath the hood, when NULLIF is used. Unbeknownst to some, NULLIF is actually a case expression under the hood, which means it is subject to data type precedence.  The second value is not always implicitly converted to the data type of the first value.  Whichever side has the less data type precedence will be converted to the other data type.  This can cause implict conversion errors, if the columns cannot be converted to the higher data type. e.g.[code]DECLARE @t TABLE(fl int);INSERT INTO @t VALUES (0);DECLARE @var CHAR(1),	@var2 DATETIMESET @var = ''SET @var2 = GETDATE()select nullif(fl, @var)FROM @t--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN [fl]=CONVERT_IMPLICIT(int,[@var],0) THEN NULL ELSE [fl] END))select nullif(fl, @var2) FROM @t--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN CONVERT_IMPLICIT(datetime,[fl],0)=[@var2] THEN NULL ELSE [fl] END))[/code]</description><pubDate>Mon, 03 May 2010 11:08:57 GMT</pubDate><dc:creator>Adam Haines</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Paul White NZ (5/3/2010)[/b][hr][quote][b]Hugo Kornelis (5/3/2010)[/b][hr]Using ISNULL can work around this - although the result is admittedly not trivial to understand:AND NULLIF(Column1, Column2) IS NULLAND NULLIF(Column2, Column1) IS NULL[/quote]:w00t:  Hugo!!!  Yuk! :sick:[/quote]Think I have to go with Paul on this one. For me, [code="sql"]WHERE Column18 &amp;gt; 27AND (Column1 = Column2 OR (Column1 IS NULL AND Column2 IS NULL))[/code]is easier to write, to read, and to understand quickly (and should perform as well or better) than:[code="sql"]WHERE Column18 &amp;gt; 27AND NULLIF(Column1, Column2) IS NULLAND NULLIF(Column2, Column1) IS NULL[/code]So I don't see any value to NULLIF in this scenario.As for me, I read the question, understood the logic, did the process, decided on NULL, then clicked on 0. Steve, when are you going to get that module that scores us on what we meant to choose, rather than what we actually chose...? :-P</description><pubDate>Mon, 03 May 2010 10:15:27 GMT</pubDate><dc:creator>sknox</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Paul White NZ (5/3/2010)[/b][hr][quote][b]Hugo Kornelis (5/3/2010)[/b][hr]Using ISNULL can work around this - although the result is admittedly not trivial to understand:AND NULLIF(Column1, Column2) IS NULLAND NULLIF(Column2, Column1) IS NULL[/quote]:w00t:  Hugo!!!  Yuk! :sick:[/quote]:Whistling:</description><pubDate>Mon, 03 May 2010 10:09:39 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Hugo Kornelis (5/3/2010)[/b][hr]Using ISNULL can work around this - although the result is admittedly not trivial to understand:AND NULLIF(Column1, Column2) IS NULLAND NULLIF(Column2, Column1) IS NULL[/quote]:w00t:  Hugo!!!  Yuk! :sick:</description><pubDate>Mon, 03 May 2010 09:55:38 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Nice question - great explanation Hugo.</description><pubDate>Mon, 03 May 2010 09:48:44 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Good question. Thanks Hugo for the detailed explanation.</description><pubDate>Mon, 03 May 2010 09:28:53 GMT</pubDate><dc:creator>rjv_rnjn</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Good question, Thanks.  I also learned a lot from the discussion, thanks Hugo for the explanation.</description><pubDate>Mon, 03 May 2010 08:47:03 GMT</pubDate><dc:creator>Trey Staker</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Thanks Hugo for the information, very useful.</description><pubDate>Mon, 03 May 2010 08:42:05 GMT</pubDate><dc:creator>Cliff Jones</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Great question. Learned two new things today:* nullif* how SQL converts an empty string to a numeric datatype.Nice one!</description><pubDate>Mon, 03 May 2010 07:31:21 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Great question, thanks.Hugo, thanks for the additional information and insight.</description><pubDate>Mon, 03 May 2010 07:30:28 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Thanks a lot Bhuvnesh!! :)</description><pubDate>Mon, 03 May 2010 05:17:52 GMT</pubDate><dc:creator>Niths</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Scott Duncan-251680 (5/2/2010)[/b][hr]Good question but just raises another for me - when would you use NULLIF?  It may be useful, but I am struggling to see where you would use it.  Why would you want something to return NULL if two values are equal?  Is it just easier than using a CASE statement?[/quote]Agreed - very good question. NULLIF is, in my opinion, one of the most under-apprecaited functions in SQL.Others have already pointed out that you can use it to avoid divide by zero errors. Another use is when a character column that is nullable has erroneously been populated with a mixture of NULLL and blank strings (instead of NULL). Now, if a report should list 'n/a' to represent the missing strings, you can use  COALESCE(NULLIF(ColumnName, ''), 'n/a')The NULLIF changes empty strings to NULL (and keeps existing NULLs as they are); the COALESCE then replaces them all with 'n/a'.A third usse is for comparing string columns that are nullable with the requirement that two NULL values should be considered equal. WHERE Column1 = Column2 will miss the NULL pairs. The usual way to work around this is to use  WHERE Column1 = Column2 OR (Column1 IS NULL AND Column2 IS NULL)This gets awkward if there are other requirements as well, because you need extra parentheses to seperate the OR from the AND, like this:  WHERE Column18 &amp;gt; 27  AND  (Column1 = Column2 OR (Column1 IS NULL AND Column2 IS NULL))Using ISNULL can work around this - although the result is admittedly not trivial to understand:  WHERE Column18 &amp;gt; 27  AND NULLIF(Column1, Column2) IS NULL  AND NULLIF(Column2, Column1) IS NULLThe first NULLIF will return NULL if both columns are equal or Column1 is NULL; the second is NULL if both are equal or Column2 IS NULL. So they are only both NULL if the columns are equal or both are NULL.As to your last question - it's not just easier than a CASE expression (not statement!), it is in fact the same. NULLIF(expr1, expr2) is defined as shorthand for CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.</description><pubDate>Mon, 03 May 2010 03:20:30 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Didn't even know such a function existed. You learn something new everyday. Now I can go home :-D</description><pubDate>Mon, 03 May 2010 01:20:19 GMT</pubDate><dc:creator>vitesh.bhana</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>i think mostly we will be using this to avoid divide by ZERO error</description><pubDate>Mon, 03 May 2010 00:55:50 GMT</pubDate><dc:creator>sharath.chalamgari</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Niths (5/2/2010)[/b][hr] But still am not clear with the output.. u are comparing 2 expressions in ur query... one is int with value 0 and the other is null... both are of different value.. then how the output is null? [/quote] in this question ' ' (blank) will be treated as INT type means 0 .So 0 equal to 0 will give expected result</description><pubDate>Mon, 03 May 2010 00:41:33 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>[quote][b]Scott Duncan-251680 (5/2/2010)[/b][hr]Good question but just raises another for me - when would you use NULLIF?  It may be useful, but I am struggling to see where you would use it.[/quote]The most common use case, in my experience, is avoiding division by 0 errors when calculating percentages, proportions, etc:[code="sql"]SELECT SomeAmount * 100.0 / NullIf(SomeTotalAmount, 0) AS SomePercentage [/code]</description><pubDate>Mon, 03 May 2010 00:20:19 GMT</pubDate><dc:creator>Tao Klerks</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Really challenging question Bhuvanesh.. :) But still am not clear with the output.. u are comparing 2 expressions in ur query... one is int with value 0 and the other is null... both are of different value.. then how the output is null? null can be output only if the 2 values taken for comparison are equal rite??!!! can u plz explain this?</description><pubDate>Sun, 02 May 2010 23:15:12 GMT</pubDate><dc:creator>Niths</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Good question but just raises another for me - when would you use NULLIF?  It may be useful, but I am struggling to see where you would use it.  Why would you want something to return NULL if two values are equal?  Is it just easier than using a CASE statement?</description><pubDate>Sun, 02 May 2010 21:55:33 GMT</pubDate><dc:creator>thecosmictrickster@gmail.com</dc:creator></item><item><title>RE: Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Cool question! Thank you. I think that nullif is one of the useful functions which is somewhat overlooked by many.Oleg</description><pubDate>Sat, 01 May 2010 17:45:09 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>Play with NULLIF</title><link>http://www.sqlservercentral.com/Forums/Topic914283-2687-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/69915/"&gt;Play with NULLIF&lt;/A&gt;[/B]</description><pubDate>Sat, 01 May 2010 17:21:37 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item></channel></rss>