Solved

Convert Char to numeric

Posted on 2009-05-04
7
865 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
[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
  • Learn & ask questions
  • 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 26

Accepted Solution

by:
Chris Luttrell earned 500 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 26

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow MySQL (InnoDB) Query with Inner Join & GroupBy 7 65
changing page verifacation 1 57
partitioning database after decade growth 8 54
Find unused columns in a table 12 67
This article describes some very basic things about SQL Server filegroups.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

740 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