Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Play with NULLIF Expand / Collapse
Author
Message
Posted Monday, May 3, 2010 2:30 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:08 AM
Points: 754, Visits: 3,816
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
Post #914946
Posted Monday, May 3, 2010 7:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 6:32 PM
Points: 11,194, Visits: 11,140
@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
Post #915032
Posted Tuesday, May 4, 2010 12:03 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 12:26 AM
Points: 2,840, Visits: 3,963
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----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #915086
Posted Tuesday, May 4, 2010 7:24 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:04 PM
Points: 1,308, Visits: 1,683
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!
Post #915281
Posted Tuesday, May 4, 2010 7:36 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:04 AM
Points: 2,351, Visits: 2,698
Great question. I learned that I should read more carefully. This question was about NULLIF (which I had never learned), not ISNULL!

Thanks,
webrunner


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"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
Post #915297
Posted Thursday, May 6, 2010 2:46 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 9:29 PM
Points: 2,644, Visits: 826
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
Post #917437
Posted Tuesday, June 22, 2010 4:18 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC 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
Post #940888
Posted Sunday, November 7, 2010 4:46 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Sunday, November 17, 2013 11:53 AM
Points: 623, Visits: 237
Awesome question. didn't know nullIf can be these much tricky...
Post #1016989
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse