Solved

SQL how to convert varchar to numeric without hanging on CAST error

Posted on 2015-01-26
5
48 Views
Last Modified: 2015-03-07
I want to do the following:

UPDATE TempTable
SET Amount = CAST(SrcAmount AS numeric(12,2))

…but I get the error
Error converting data type varchar to numeric
on some value, but they all look valid.  This suggests that there’s an obscure, non-printing ASCII character hidden in one of the SrcAmount values.

Is there some way I can modify the SQL command above to ignore the CAST error and simply skip over those records which generate it?
0
Comment
Question by:tesupport
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40571350
Run this to flush out any values that cannot be converted to a numeric data type, and see what is returned
SELECT SrcAmount 
FROM TempTable 
WHERE ISNUMERIC(SrcAmount) = 0

Open in new window


>This suggests that there’s an obscure, non-printing ASCII character hidden in one of the SrcAmount values.
Possibly.  In those situations I usually grab the source file, open in a super-dooper text editor like UltraEdit, ctrl-H to go into Hex mode, and find the ASCII code(s) of the offending character(s).  Then you can weed them out.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40571375
If you're using SQL Server 2012/14, you can use TRY_CAST
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40571381
If not, then I would reverse Jim's suggestion :-)

UPDATE TempTable
SET Amount = CAST(SrcAmount AS numeric(12,2))
Where ISNUMERIC(SrcAmount) <> 0
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40572217
Can be the decimal point. Which character are you using for decimal point? Dot or comma?
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40573149
ISNUMERIC() has to support a very wide range of data types, and so it is not necessarily reliable for numeric/decimal data.  For example:
SELECT ISNUMERIC('1e1'),ISNUMERIC('$1')


Code below assumes you do not have a minus/negative sign in any value.  If you do, let me know, we can adjust the code for that.

UPDATE TempTable
 SET Amount = CAST(SrcAmount AS numeric(12,2))
WHERE LTRIM(RTRIM(SrcAmount)) NOT LIKE '%[^0-9.]%'
  AND LTRIM(RTRIM(SrcAmount)) NOT LIKE '%.%.%'
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Haw to apply join on 2 tables with this scenario 4 22
Sql query 34 35
How to SQL Trace a SPECIFIC query 24 59
DATETIMEOFFSET feature 1 33
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now