Datatype conversion error-SQL Server Query

I am getting below error in SQL Server, Can you advise how to fix below error.
SELECT 
	   AVG(CAST(COL1 as decimal)) A 
FROM TBL 

Open in new window

Error: Error converting data type varchar to numeric.

COL1 datatype is varchar (256)
dbaoracle30092015Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jim HornMicrosoft SQL Server Data DudeCommented:
There is a value in COL1 somewhere that can't be converted to a decimal, such as 'banana', '3+5' or ' ', and you can't perform a mathematical aggregate like AVG on 'banana'.

Run this SQL statement using ISNUMERIC and see what values can't be converted...
SELECT COL1 FROM TBL WHERE ISNUMERIC(COL1) = 0

Open in new window

dbaoracle30092015Author Commented:
The above query is returning 9 rows as blank (there is no value)
Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT AVG(CAST(COL1 as decimal)) A 
WHERE ISNULL(COL1,'') <> ''
FROM TBL 

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

dbaoracle30092015Author Commented:
I got below error: Incorrect syntax near the keyword 'FROM'.
Jim HornMicrosoft SQL Server Data DudeCommented:
>The above query is returning 9 rows as blank (there is no value)
In that case you'll need to weed out the blanks.  If it's just one space then give this a whirl..

SELECT AVG(CAST(COL1 as decimal)) A 
FROM TBL 
WHERE ISNUMERIC(COL1) =1 AND ISNULL(COL1, '') <> '') 

Open in new window


If multiple blanks, you may have to do this..
SELECT AVG(CAST(COL1 as decimal)) A 
FROM TBL 
WHERE ISNUMERIC(COL1) =1 AND ISNULL(LTRIM(RTRIM(COL1)), '') <> '') 

Open in new window

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Sorry, I just swaped FROM and WHERE positions:
SELECT AVG(CAST(COL1 as decimal)) A 
FROM TBL 
WHERE ISNULL(COL1,'') <> ''

Open in new window

Jim HornMicrosoft SQL Server Data DudeCommented:
This also begs the question ... Why are numbers being inserted into a varchar(255) column?  If the intent is to do math on them then it should be numeric in the first place, avoiding a host of issues like this.
dbaoracle30092015Author Commented:
--
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.