Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my script but its giving me error as
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.

script

SQL
create table #temp ( strAmount varchar(50))
  insert into #temp values('123A')
  insert into #temp values('123.00')
  select * from #temp

  select
 CASE
    When ISNUMERIC(strAmount)=1 then 
       CONVERT(Numeric(10,2),strAmount)
    else 
       strAmount
  End
 from #temp
Posted
Comments
Monicavensaslas 27-Mar-15 5:53am    
What is your requirement.. you want to convert strAmount into which datatype??
vishal_h 27-Mar-15 5:56am    
I want to display value of that varchar column is if numeric then show in numeric(18,2) format otherwise whatever be the value just show.whether it is varchar or anything.

So start by looking at your data.
SQL reports "Error converting data type varchar to numeric" when the data in your column cannot be converted to a number because it contains characters that are not numeric digits: 'A' to 'Z' for example.

Then, when you have found the "bad" data - change your database so that you store numeric values in numeric columns. If you don't, then this problem will come back - again, and again, and again. Never use VARCHAR or NVARCHAR columns except of alphabetic data. Dates in DATE columns, number is numeric columns. Otherwise you are just storing up problems for yourself later as you are starting to find here!
 
Share this answer
 
Comments
vishal_h 27-Mar-15 5:56am    
I want to display value of that varchar column is if numeric then show in numeric(18,2) format otherwise whatever be the value just show.whether it is varchar or anything.
OriginalGriff 27-Mar-15 6:10am    
You need to look at your data: ISNUMERIC does not guarantee that the string is convertable, just that it's a number. So if it's currency for example it will pass ISNUMERIC and fail CONVERT.

https://msdn.microsoft.com/en-us/library/ms186272.aspx

Seriously: if it's supposed to be a number, store it in a number!
SQL
create table #temp ( strAmount varchar(50))
  insert into #temp values('123A')
  insert into #temp values('123.00')
  select * from #temp

  select
 CASE
    When ISNUMERIC(strAmount)=1 then
       CONVERT(varchar(50),CONVERT(Numeric(10,2),strAmount))
    else
       strAmount
  End
 from #temp
 
Share this answer
 
As you are inserting monetary value in the table,
ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0. Valid numeric data types are

VB
int

numeric

bigint

money

smallint

smallmoney

tinyint

float

decimal

real


Ref: https://msdn.microsoft.com/en-us/library/ms186272.aspx[^]



so isnumeric returns false for the first item and true for the second.

When it is true, you are trying to convert it to number whereas the actual value is not really a number as it contains '$' char. so the conversion fails and you will get error.


Try the following query

SQL
select
CASE
   When ISNUMERIC(strAmount)=1 then
      CONVERT(varchar(50),CONVERT(numeric(18,2),CONVERT(Money,strAmount)))
   else
      strAmount
 End
from #temp


Here first we are converting the data to money and then to a required number format.

Another thing to note here is you need to convert the resulting number to varchar because we are returning the non-numeric values also in this query. so you need to use
CONVERT(varchar(50),CONVERT(numeric(18,2),CONVERT(Money,strAmount)))
if you use
CONVERT(numeric(18,2),CONVERT(Money,strAmount))

again you will get error because the query returns different data types...i.e. if it is numeric, it returns a number else varchar. so you need to convert the resulting number to varchar.

Hope I'm clear.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900