Solved

# convert varchar to numeric help

Posted on 2014-09-16
178 Views
Hi

the following fails:

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

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

any idea what i'm doing wrong?
0
Question by:websss
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2

LVL 24

Accepted Solution

Phillip Burton earned 500 total points
ID: 40324883
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

LVL 24

Expert Comment

ID: 40324884
or

declare @vOdometer varchar(50) = 419

print convert(numeric,@vOdometer) * 0.1
0

Author Comment

ID: 40324985
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

LVL 24

Expert Comment

ID: 40324989
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 Comment

ID: 40324990
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

LVL 24

Expert Comment

ID: 40324991
The minimum you need. How many digits do you need (precision)? How many digits after the decimal point do you need (scale)?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includâ€¦
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
###### Suggested Courses
Course of the Month7 days, 14 hours left to enroll