Solved

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

Posted on 2015-01-26
5
53 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 48

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

837 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