Solved

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

Posted on 2015-01-26
5
44 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 45

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:
ScottPletcher 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

15 Experts available now in Live!

Get 1:1 Help Now