Solved

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

Posted on 2015-01-26
5
54 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
[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
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 49

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help how to find where my error is in UFD 6 47
convert null in sql server 12 58
Stored Proc - Rewrite 42 73
SQL server client app 3 32
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …

726 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