Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert Char to numeric

Posted on 2009-05-04
7
Medium Priority
?
874 Views
Last Modified: 2013-11-15
I have a char field that stores a numeric value in it 128,074.  I want to update this value in a stored procedure by multiplying by .015. So far I am unable to convert to numeric. I receive this error Error converting data type varchar to numeric.
0
Comment
Question by:NCLGS
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:rem1010
ID: 24296222
I have attached a very simple code snippet using Perl. However, the concept in most languages is similar.
First, convert the field to numeric like this Perl builtin function, which will result in ONLY numbers, no commas, no decimal points, no spaces, just 0-9
$Numbfield =~ s/\D+//g;
If you need to have decimal points and negative signs, then perl has a substitution such as:
$Numbfield =~ s/[0-9\.\,\-]//g;
The backslashes in perl are used to escape the value so that a "," is meant to be a "," and not a field delimiter, etc.

Since you did not specify what platform, language, or database, I utilized Perl which is cross platform.

#!/usr/bin/perl -w
my $Textfield = qq[128,074];;
my $Numbfield;
my $Answer;
my $Multiplier = '.015';
$Numbfield = $Textfield;
print qq[numbfield is $Numbfield\n];
$Numbfield =~ s/\D+//g;
print qq[numbfiled num is $Numbfield\n];
print qq[multi is $Multiplier\n];
$Answer = $Numbfield * $Multiplier;
 
print qq[answer is  $Answer\n];

Open in new window

0
 
LVL 3

Expert Comment

by:rem1010
ID: 24296313
Sorry, missed your "Stored Procedure" statement.
A simple simple solution is to perform a function on that data WHEN it is stored into the database and store the ACTUAL value in both the Varchar and an INT field, if the varchar field is required.
Sure it is not ultra dramatic, but it would solve the problem in a quick and inexpensive timely manner.
Take a look at processing that value WHEN it is acquired rather than AFTER.

0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 2000 total points
ID: 24563793
The problem is the implicit conversion of a varchar/char into a numeric will not handle the commas in the string.  You have to remove them either with Replace or the Money conversion has a style to do it.
The code snippet below shows the two options for converting your string value of '128,074' into a numeric value to use in your equation.
DECLARE @v VARCHAR(50), @multiplier NUMERIC(18,4);
SET @v = '128,074';
SET @multiplier = 0.15;
 
SELECT @v, @multiplier, CONVERT(NUMERIC(18,4),CONVERT(money,@v,1))*@multiplier AS Answer
 
SELECT @v, @multiplier, CONVERT(NUMERIC(18,4),REPLACE(@v,',',''))*@multiplier AS Answer

Open in new window

0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 25225093
What is the soulution you "found" and how is it different from something presented here?
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question