Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coalesce VS ISNULL in a group by clause

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello,

I am trying to run this query to find all instances where booktype + '_' + bookno exist more than once. Sometimes one of the fileds will be NULL so I did this:

Code:
select * from mytable
where coalesce(booktype, '') + '_' + coalesce(bookno, '') 
in (select coalesce(booktype, '') + '_' + coalesce(bookno, '') from mytable
group by coalesce(booktype, '') + '_' + coalesce(bookno, '') 
having count(coalesce(booktype, '') + '_' + coalesce(bookno, ''))>1)

When running this query I get the following messages:

Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookType' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookType' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'mytable.bookNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.



However, If I use isnull instead of coalesce, the query will run just fine:

Code:
select * from mytable
where isnull(booktype, '') + '_' + isnull(bookno, '') 
in (select isnull(booktype, '') + '_' + isnull(bookno, '') from mytable
group by isnull(booktype, '') + '_' + isnull(bookno, '') 
having count(isnull(booktype, '') + '_' + isnull(bookno, ''))>1)


Is there a reason for this? Is coalesce picking up something that ISNULL does not pick up?

Thanks!

Brian
 
There is nothing that I am aware of that would account for this problem.

One big difference between Coalesce and IsNull is the returned data type. Coalesce will examine the data types of all of the parameters and use data type precedence to determine the returned data type. IsNull always returns the data type of the first parameter.

Here is an example:

Code:
Declare @A BigInt,
        @B Int

Select  @A = 1,
        @B = 2

Select	SQL_VARIANT_PROPERTY(Coalesce(@A, @B), 'BaseType') -- BigInt
Select	SQL_VARIANT_PROPERTY(Coalesce(@B, @A), 'BaseType') -- BigInt

Select	SQL_VARIANT_PROPERTY(IsNull(@A, @B), 'BaseType')   -- BigInt
Select	SQL_VARIANT_PROPERTY(IsNull(@B, @A), 'BaseType')   -- [!]Int[/!]

Do you get any errors if you run this?

Code:
select coalesce(booktype, '') + '_' + coalesce(bookno, '') from mytable
group by coalesce(booktype, '') + '_' + coalesce(bookno, '') 
having count(coalesce(booktype, '') + '_' + coalesce(bookno, ''))>1

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks gmmastros.

Nope, I didn't get an error with that script. I'm guessing Coalesce did something with the data type.
 
How many columns are in mytable - just the two?

----------------------------------------

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind - Bernard Baruch

Computer Science is no more about computers than astronomy is about telescopes - EW Dijkstra
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top