September 30, 2015 at 2:25 pm
My bad... :w00t: You are correct that Try_Parse will return 0 for '+' (and also for '-'), as these are effectively treated as "+0" and "-0".
I should have used '$' instead (and should have actually run the code first!).
I do like Try_Convert a lot, as it eliminates all the extra code I had to write to determine whether an input string was really a number.
Also - if you are inside a procedure within a transaction and use IsNumeric and then try to assign the value to an Int variable, even if you use a Try..Catch block to trap the error you will cause the transaction to fail to an uncommittable state when the input value passes IsNumeric but then fails the Int assignment. This doesn't happen with Try_Convert.
October 1, 2015 at 3:21 am
Luis Cazares (9/30/2015)
When I use Try_Convert(int, '+') it gives me 0 instead of NULL.
That's probably because '+' converts to 0 using regular CONVERT, so it doesn't count as a value that can't be converted and would return NULL. Doing a TRY_CONVERT with '$' returns NULL, whereas regular CONVERT throws an error.
[EDIT] Whoops, didn't switch to the next page and see Kim had already pointed that out, sorry!
Viewing 2 posts - 31 through 31 (of 31 total)
You must be logged in to reply to this topic. Login to reply