Home All Groups Group Topic Archive Search About

How to trap an error from insert ?

Author
20 Aug 2005 3:52 AM
Tam Vu
When inserting rows from a staging table to a production one, I need to
convert a column of type varchar to type int. Often there're rows that
have junk data in this column and that makes convert() fail. Is it
possible to know that such junk rows exist without getting an error
message ? In other words, is there a way to prevent the insert query
from throwing an error msg, but I still know that it fails ?
thanks,

Tam

Author
20 Aug 2005 6:25 AM
Roji. P. Thomas
You can use the ISNUMERIC() to determine whether an
expression is a valid numeric type.

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


Show quote
"Tam Vu" <vuht2***@yahoo.com> wrote in message
news:1124509954.485547.268620@o13g2000cwo.googlegroups.com...
> When inserting rows from a staging table to a production one, I need to
> convert a column of type varchar to type int. Often there're rows that
> have junk data in this column and that makes convert() fail. Is it
> possible to know that such junk rows exist without getting an error
> message ? In other words, is there a way to prevent the insert query
> from throwing an error msg, but I still know that it fails ?
> thanks,
>
> Tam
>
Author
20 Aug 2005 3:13 PM
Tam Vu
I've tried isnumeric() - indeed this was the first thing I did. Yet
strings that have character like 'd', 'e', '.' also pass isnumeric()
but are not convertible to int, and these characters happen quite
commonly in the junk rows in my DB. Any other suggestions ?
Author
21 Aug 2005 1:30 AM
--CELKO--
You can check for all digits with:

CASE WHEN
REPLACE(
  REPLACE(
   ..
    REPLACE (num '0',''),
   1, ''),
..
9, '')  = '' THEN CAST(num AS INTEGER) ELSE NULL END
Author
21 Aug 2005 12:07 PM
Hugo Kornelis
On 20 Aug 2005 18:30:21 -0700, --CELKO-- wrote:

>You can check for all digits with:
>
>CASE WHEN
>REPLACE(
>  REPLACE(
>   ..
>    REPLACE (num '0',''),
>   1, ''),
> ..
> 9, '')  = '' THEN CAST(num AS INTEGER) ELSE NULL END

Hi Joe,

Never rely on implicit conversion if you don't have to. Use quotes
around 1, 2, ..., 9 as well to prevent conversions.

And of course, this is lots more complicated then encessary:

CASE WHEN num NOT LIKE '%^[0-9]%' THEN CASE(num AS INTEGER) END

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Author
22 Aug 2005 12:02 AM
Tam Vu
these are interesting solutions. btw, is there any "try-catch"
structure in sql server as I'm concerned there're cases that exceptions
are not known before hand ?

thanks,

Tam
Author
22 Aug 2005 5:36 AM
Roji. P. Thomas
Yep. But in SQL Server 2005

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


Show quote
"Tam Vu" <vuht2***@yahoo.com> wrote in message
news:1124668962.565306.81090@g43g2000cwa.googlegroups.com...
> these are interesting solutions. btw, is there any "try-catch"
> structure in sql server as I'm concerned there're cases that exceptions
> are not known before hand ?
>
> thanks,
>
> Tam
>
Author
22 Aug 2005 5:36 AM
Roji. P. Thomas
>> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CASE(num AS INTEGER) END
Should be
> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CAST(num AS INTEGER) END

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


Show quote
"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:kgrgg11r74k425gau3958b7dgjjtcsu9vu@4ax.com...
> On 20 Aug 2005 18:30:21 -0700, --CELKO-- wrote:
>
>>You can check for all digits with:
>>
>>CASE WHEN
>>REPLACE(
>>  REPLACE(
>>   ..
>>    REPLACE (num '0',''),
>>   1, ''),
>> ..
>> 9, '')  = '' THEN CAST(num AS INTEGER) ELSE NULL END
>
> Hi Joe,
>
> Never rely on implicit conversion if you don't have to. Use quotes
> around 1, 2, ..., 9 as well to prevent conversions.
>
> And of course, this is lots more complicated then encessary:
>
> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CASE(num AS INTEGER) END
>
> Best, Hugo
> --
>
> (Remove _NO_ and _SPAM_ to get my e-mail address)
Author
22 Aug 2005 10:58 PM
Hugo Kornelis
On Mon, 22 Aug 2005 11:06:30 +0530, Roji. P. Thomas wrote:

>>> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CASE(num AS INTEGER) END
>Should be
>> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CAST(num AS INTEGER) END

Hi Roji,


You are correct that I made a mistake. But your correction is wrong too
(since it's an exact same copy - you obviously forgot to correct the
mistake before posting).

For others reading this discussion: the correct statement is

>> CASE WHEN num NOT LIKE '%[^0-9]%' THEN CAST(num AS INTEGER) END

(Note how the caret has sneaked one position to the right)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Author
23 Aug 2005 4:52 AM
Roji. P. Thomas
> You are correct that I made a mistake. But your correction is wrong too
> (since it's an exact same copy - you obviously forgot to correct the
> mistake before posting).

:)

In a hurry to correct you asap, I refuse to look at the finer details :)

Infact I have'nt noticed the misplaced caret, but only the mispelled CAST.

Lets keep correcting each other :p

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com


Show quote
"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:m1mkg19d2vmgq7vvus5ncer3di83gdvhut@4ax.com...
> On Mon, 22 Aug 2005 11:06:30 +0530, Roji. P. Thomas wrote:
>
>>>> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CASE(num AS INTEGER) END
>>Should be
>>> CASE WHEN num NOT LIKE '%^[0-9]%' THEN CAST(num AS INTEGER) END
>
> Hi Roji,
>
>
> You are correct that I made a mistake. But your correction is wrong too
> (since it's an exact same copy - you obviously forgot to correct the
> mistake before posting).
>
> For others reading this discussion: the correct statement is
>
>>> CASE WHEN num NOT LIKE '%[^0-9]%' THEN CAST(num AS INTEGER) END
>
> (Note how the caret has sneaked one position to the right)
>
> Best, Hugo
> --
>
> (Remove _NO_ and _SPAM_ to get my e-mail address)
Author
23 Aug 2005 8:21 PM
Hugo Kornelis
On Tue, 23 Aug 2005 10:22:02 +0530, Roji. P. Thomas wrote:

>> You are correct that I made a mistake. But your correction is wrong too
>> (since it's an exact same copy - you obviously forgot to correct the
>> mistake before posting).
>
>:)
>
>In a hurry to correct you asap, I refuse to look at the finer details :)
>
>Infact I have'nt noticed the misplaced caret, but only the mispelled CAST.

Hi Roji,

Would you believe that I looked at my original post and your correction
several times, comparing them character by character, and ended up not
spotting the difference?

This is so embarrassing - I *know* that my finger always want to type an
E after CAS; it's the single most common syntax error in my queries - I
really should have checked that part better when I saw your correction.

>Lets keep correcting each other :p

I'll be keeping a close eye on your posts! ;->

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

AddThis Social Bookmark Button