Substituting a 'NULL' as none

Hello, I am trying to subsititute NULL results and replace them as none.

coalesce(cast(avg(purchase_price)as varchar(4)), 'none')

This statement gives me an error.

Arithmetic overflow error converting numeric to data type varchar.

Any idea on what I am doing incorrectly?

Thanks.
Jintonix415Asked:
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.

Anthony PerkinsCommented:
Please post your entire query so that we can suggest a workaround.
0
Jintonix415Author Commented:
Select  M.menu_id, 
		menu_desc,
		Count(*) As NumberofMenus,
		AVG(purchase_price) As AvgPurchasePrice,
		coalesce(cast(avg(purchase_price)as varchar(4)), 'none')  as AvgCostPrice
From	Menu M join menu_content MC on M.Menu_id = MC.menu_id
		join items I on MC.item_id = I.item_id
Group by M.menu_id, menu_desc 

Open in new window


If I remove the

coalesce(cast(avg(purchase_price)as varchar(4)), 'none')  as AvgCostPrice

It is fine.
0
Anthony PerkinsCommented:
You need to make the varchar size larger as in:
coalesce(cast(avg(purchase_price)as varchar(20)), 'none')  as AvgCostPrice
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jintonix415Author Commented:
Ahhh thank you.  Do you also happen to know how to round out to two decimal places for that particular line? If you prefer I can create a new question for this, thank you!
0
Anthony PerkinsCommented:
See if this works for you:
SELECT  M.menu_id,
        menu_desc,
        COUNT(*) AS NumberofMenus,
        AVG(purchase_price) AS AvgPurchasePrice,
        COALESCE(CAST(ROUND(AVG(purchase_price), 2) AS varchar(20)), 'none') AS AvgCostPrice
FROM    Menu M
        JOIN menu_content MC ON M.Menu_id = MC.menu_id
        JOIN items I ON MC.item_id = I.item_id
GROUP BY M.menu_id,
        menu_desc

Open in new window

0
Jintonix415Author Commented:
Thanks, but the results show as 45.260000. I would like to get rid of the 4 zeros to the right.
0
Anthony PerkinsCommented:
>>I would like to get rid of the 4 zeros to the right. <<
Than you need to be more precise:  You not only want to round you also want to truncate trailing zeros as in something like this:
SELECT  M.menu_id,
        menu_desc,
        COUNT(*) AS NumberofMenus,
        AVG(purchase_price) AS AvgPurchasePrice,
        COALESCE(CAST(CAST(AVG(purchase_price) AS decimal(20,2)) AS varchar(20)), 'none') AS AvgCostPrice
FROM    Menu M
        JOIN menu_content MC ON M.Menu_id = MC.menu_id
        JOIN items I ON MC.item_id = I.item_id
GROUP BY M.menu_id,
        menu_desc

Open in new window

0
Jintonix415Author Commented:
Wow, genius indeed. Thank you!!!
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

From novice to tech pro — start learning today.