Outputting a blank as a field value if formula = 0

I have created a SQL statement to calculate the difference between two numerical fields.  Sometimes the field values are null, so I coalesce the values to zero.  If the calculated difference is zero, the output should be a blank and not a zero.

Here is the statement:

convert(varchar,case when coalesce(Debit,0)-coalesce(Credit,0) > 0 then coalesce(Debit,0)-coalesce(Credit,0) else '' end ) as DR
,case when coalesce(Debit,0)-coalesce(Credit,0) < 0 then (coalesce(Credit,0)-coalesce(Debit,0)) else '' end as CR
Here is an example of the output:

AccountID      AccountDescription                                                      DR      CR
01.6205.2010      PTM MEDICAL/SURGICAL UNI OP MEDICARE      0.00      4180.50

How can I make the DR value ' '?

Thanks

Glen
GPSPOWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell FoxDatabase DeveloperCommented:
You just need to move your CONVERT to the calculation so all possible outcomes are of the same data type; i.e., the case statement can only produce strings or it can only produce numbers:
		case when coalesce(Debit,0)-coalesce(Credit,0) > 0 
			then convert(varchar, COALESCE(Debit,0)-coalesce(Credit,0))
			else '' 
		END 
	as DR
,		CASE when coalesce(Debit,0)-coalesce(Credit,0) < 0 
			THEN (coalesce(Credit,0)-coalesce(Debit,0)) 
			ELSE '' 
		END 
	AS CR

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jose TorresSenior SQL Server DBACommented:
Russell is correct that both results of the case need to be the same data type.
I would like to add why use coalesce I would use ISNULL(Debit,0)
0
GPSPOWAuthor Commented:
Thank you

Glen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.