SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Catch numeric conversion errors and KEEP GOING


Catch numeric conversion errors and KEEP GOING

Author
Message
cvgalante
cvgalante
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 76
I'm pretty frustrated! I am working with the data back end in an existing program that stores student marks (A, B, I, P, F, or numbers and some other values). I cannot control what the users put in the Marks column which is VARCHAR(10). I need to produce a query that compares numeric marks against certain values and ignores everything else - for instance, MARK > 100. It should be a single SELECT query, because the program's custom report generator simple executes that and returns the columns. However, I may be able to execute a stored procedure that returns a table with the report generator if necessary.

My problem is this: T-SQL ISNUMERIC function considers certain things numeric that are not. For instance, a hyphen. However, it will subsequently fail to convert or cast properly to a numeric data type. Most error trapping methods, like @@ERROR or TRY...CATCH, will stop the processing, in which case the custom report gives the user nothing (except the message "No tables in dataset"). I can't use TRY...CATCH within a user-defined function either. I don't want to just send the user an error message.

How can I construct a query that will simply ignore those records that fail to convert and produce a result? Also, how do I pinpoint the records that are causing the problem and perhaps report those to the user? I'm going to hate myself if I am missing something simple here....
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39782 Visits: 38563
It would help if you could provide the table DDL (create statement), some sample data (in the form of insert statements), and what your expected results would be based on the sample data.

If you take a few minutes to read the article referenced below regarding Asking for assistance, and follow the guidelines in the article, we can probably help you out relatively easily.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
cvgalante
cvgalante
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 76
Thanks Lynn. I was thinking about not revealing sensitive data, but let's look at a simple example:
CREATE TABLE #MARKTABLE
(STUNAME VARCHAR(50),
MARK VARCHAR(10))
INSERT #MARKTABLE VALUES ('TOM','95.5')
INSERT #MARKTABLE VALUES ('DICK','101')
INSERT #MARKTABLE VALUES ('HARRY','-')

Now we execute:
SELECT * FROM #MARKTABLE
WHERE ISNUMERIC(MARK)=1

And we get:
TOM 95.5
DICK 101
HARRY -

But execute this:
SELECT * FROM #MARKTABLE
WHERE ISNUMERIC(MARK)=1 AND CAST(MARK AS NUMERIC)>100.0

And the result is:
Msg 8115, Level 16, State 6, Line 2
Arithmetic overflow error converting varchar to data type numeric.

I want one query (or perhaps a procedure) that ignores the hyphen and produces this:
DICK 101
since Dick is the only student with a Mark over 100. I also need to check for marks > 4, marks = 0, etc. Why does T-SQL not have a real ISNUMERIC function that can tell if a conversion will work?

Thanks,
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87130 Visits: 41113
SELECT * FROM #MARKTABLE
WHERE MARK NOT LIKE '%[^0-9.]%'



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
cvgalante
cvgalante
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 76
Well, straightforward enough! I figured I was missing something fairly simple. Thanks!
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87130 Visits: 41113
Heh... thanks for the feedback... didn't mean for it to be so stark. Hehe Any questions on how that works?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
cvgalante
cvgalante
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 76
No, I get it. I really just haven't paid enough attention to the pattern matching options in LIKE. Plus, I got hung up on trying to work the ISNUMERIC function and didn't give any thought to analyzing the contents of the field. Stark or not, the help is always appreciated! Wink
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search