# convert varchar to numeric help

Hi

the following fails:

``````	   declare @vOdometer varchar(50) = 419

print convert(numeric,(@vOdometer * 0.1))
``````

any idea what i'm doing wrong?
###### 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.

Director, Practice Manager and Computing ConsultantCommented:
You've got the brackets in the wrong place.

You want to convert @vOdometer to numeric, THEN multiply by 0.1. What you are trying to do it is force an implicit conversion, then an explicit conversion.

Try this:

declare @vOdometer varchar(50) = 419

print convert(numeric,(@vOdometer)) * 0.1
0

Experts Exchange Solution brought to you by

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

Director, Practice Manager and Computing ConsultantCommented:
or

declare @vOdometer varchar(50) = 419

print convert(numeric,@vOdometer) * 0.1
0
Author Commented:
Thanks
Thats works

But when i try and have it in the SP it missing the .9
the column is :
vOdometer (Numeric(18,0),Null)
0
Director, Practice Manager and Computing ConsultantCommented:
That's because you are using Numeric(18,0) - that means, 18 digits in total, 0 after the decimal point. See http://msdn.microsoft.com/en-gb/library/ms187746.aspx for details.

Therefore, you need to use Numeric(18,1)

(If you need to use 18 in the first place - that sounds like overkill).
0
Author Commented:
hmm, i think the column name is wrong
when i manually edit the table and adjust the row it doesn't work either
which data type should i be storing it as?

I guess i'd be storing xx.xx
0
Director, Practice Manager and Computing ConsultantCommented:
The minimum you need. How many digits do you need (precision)? How many digits after the decimal point do you need (scale)?
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 2005

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.