|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Today @ 10:04 AM
Points: 753,
Visits: 3,786
|
|
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?
______________________________________________________________________ The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 2:43 PM
Points: 10,990,
Visits: 10,576
|
|
@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 reply. (I don't disagree with anything you wrote by the way)
Paul
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 7:03 AM
Points: 2,562,
Visits: 3,453
|
|
Adam Haines (5/3/2010)
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. DECLARE @t TABLE(fl int);
INSERT INTO @t VALUES (0);
DECLARE @var CHAR(1), @var2 DATETIME SET @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))
thats called having red meat with cold beer on beach....PERFECT fruit for this thread.
-------Bhuvnesh---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 12:28 PM
Points: 1,058,
Visits: 1,394
|
|
Paul White NZ (5/3/2010)
@sknox: I laughed when I saw Hugo's  reply. Paul
So did I ... AFTER I'd already posted my reply. Oh, the wonders of asynchronous operations!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 2:08 PM
Points: 2,121,
Visits: 2,226
|
|
Great question. I learned that I should read more carefully. This question was about NULLIF (which I had never learned), not ISNULL! 
Thanks, webrunner
------------------- "The chemistry must be respected." - Walter White
"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'" Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, June 06, 2013 8:37 PM
Points: 2,609,
Visits: 768
|
|
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.
Scott Duncan
MARCUS. Why dost thou laugh? It fits not with this hour. TITUS. Why, I have not another tear to shed; --Titus Andronicus, William Shakespeare
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Wednesday, June 30, 2010 2:54 AM
Points: 78,
Visits: 9
|
|
When you cast(blank('') as int), it returns 0. so when you compare 0 with 0 nullif returns NULL. declare @a smallint set @a= 0 select nullif(@a,0)
Ans: NULL
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, July 25, 2012 9:04 PM
Points: 542,
Visits: 187
|
|
| Awesome question. didn't know nullIf can be these much tricky...
|
|
|
|