Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 When do null values cause problems.

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-19 : 14:34:24
Just noticed something.

I use scripts like this typically to remove duplicate values between tables based on a predetermined set of common fields:



Delete D FROM
(
SELECT Firstname, Middlename, Lastname, DOB, RaceID, Height, ROW_NUMBER()OVER(ORDER BY Firstname, Middlename, Lastname, DOB, RaceID, Height) AS RowNum
FROM dbo.ARR_PC)D
JOIN
(
SELECT Firstname, Middlename, Lastname, DOB, RaceID, Height, ROW_NUMBER()OVER(ORDER BY Firstname, Middlename, Lastname, DOB, RaceID, Height) AS RowNum
FROM dbo.ARR_PC
)E
on D.Firstname = E.Firstname
AND D.Middlename=E.Middlename
AND D.Lastname=E.Lastname
AND D.RowNum < E.RowNum
AND D.DOB=e.DOB
AND d.RaceID=e.RaceID
GO


and i use scripts like this to typically find entries in one table that do not exist in another (or vice versa):


SELECT *
FROM douglasmni.dbo.mni t1
INNER JOIN Douglas.dbo.persondata t2
ON t1.FNAME = t2.FNAME
AND T1.MNAME = T2.MNAME
AND T1.LNAME = T2.LNAME
AND t1.dob = t2.dob
ORDER BY t1.LNAME


or


SELECT * FROM dbo.MNI_TO_IMPORT_NO_ALIASES t1
WHERE MNINO NOT IN (SELECT MNINO FROM MNI t2 WHERE t2.MNINO=t1.MNINO)


I've noticed that executing any of these will provide me with a positive set of results, but, upon closer inspection, it's actually missing entries, whether it be duplicates or entries which exist in one table but not another. In any of the fields looked at, the potential for genuine NULL values exists.

For this reason, in these sorts of scripts, do i need to ALWAYS use COALESCE or wrap them line in ISNULL to get true results? And is there any benefit to one over another?

Thanks

james

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 14:57:43
yep...or use explicitly checks using is null or is not null operators
all except these operators will ignore NULL values under default condition as NULL is not regarded as a value but an undefined or unknown value instead

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-19 : 15:00:09
no discernable benefit between coalesce and isnull in this process?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-19 : 15:49:07
Suuuuuuure there is....

One is ANSI Compatible....hence you are expanding your knowledge to be able to do cross platform developement

The other....is not

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-03-19 : 15:55:40
COALESCE is ANSI/ISO standard. ISNULL is a T-SQL feature that does the same thing as COALESCE, except it only accepts 2 parameters.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 15:56:08
quote:
Originally posted by WJHamel

no discernable benefit between coalesce and isnull in this process?


not much from performance aspect

but there are few differences

see

http://beyondrelational.com/blogs/dave_ballantyne/archive/2010/05/19/null-or-coalesce-what-s-the-difference.aspx

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-19 : 16:01:24
yeah, i've seen the ANSI comparison between the two. I wasn't aware that COALESCE would accept more than two params. What would the purpose be of putting in more than 2?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-19 : 16:05:20
quote:
Originally posted by WJHamel

yeah, i've seen the ANSI comparison between the two. I wasn't aware that COALESCE would accept more than two params. What would the purpose be of putting in more than 2?


to get the first not null value out of them

See a practical example like
ContactName HomePhone WorkPhone CellPhone

and all phone fields are optional

so in such case we can get whatever phone number is present by using


COALESCE(HomePhone,WorkPhone,CellPhone)

based on order of preference

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-19 : 16:47:09
Ah. Very cool.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-19 : 21:53:03
COALESCE(Girl1, Girl2, Girl3, Girl4, Girl5, Girl6, Girl7, ect)



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-20 : 06:25:28
Ha!
Go to Top of Page
   

- Advertisement -