Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234»»

IsNumeric with NULL and Empty String Expand / Collapse
Author
Message
Posted Thursday, August 4, 2011 9:06 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 1,345, Visits: 1,717
SQLkiwi (8/4/2011)
IsNumeric - sigh.

Thank goodness for the improvements in the next version of SQL Server like TRY_CONVERT.


TRY_CONVERT - sigh.

While it's in many ways better than IsNumeric, TRY_CONVERT does have some problems. Take this example from the documentation:

SELECT 
CASE WHEN TRY_CONVERT(float,'test') IS NULL
THEN 'Cast failed'
ELSE 'Cast succeeded'
END AS Result

Now try:
SELECT 
CASE WHEN TRY_CONVERT(float,NULL) IS NULL
THEN 'Cast failed'
ELSE 'Cast succeeded'
END AS Result

Did the second cast fail? You don't really know, because NULL is returned when the cast fails, but also when the cast succeeds and the result of the cast is NULL.
Post #1154315
Posted Thursday, August 4, 2011 9:35 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 11:02 AM
Points: 17,843, Visits: 15,792
Thanks for the question



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #1154346
Posted Thursday, August 4, 2011 11:07 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 4:46 PM
Points: 1,920, Visits: 582
Nice question.

-----------------
Gobikannan
Post #1154445
Posted Thursday, August 4, 2011 11:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:02 AM
Points: 1,258, Visits: 13,556
nice!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
Post #1154468
Posted Thursday, August 4, 2011 12:01 PM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:15 AM
Points: 1,447, Visits: 1,059
Good question. Reviewed some basic concepts. Obviously, I needed this refresher because I got it right for the wrong reason, too.
Post #1154480
Posted Thursday, August 4, 2011 12:03 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,228, Visits: 1,046
Nice question. Simple and makes a good point on Conversions.
Post #1154481
Posted Thursday, August 4, 2011 12:10 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,228, Visits: 1,046
Daniel Bowlin (8/4/2011)
Implicit conversion is one of those issues, where if you know it, you just know. If you don't know it, the result is very unexpected. I am learning more and more of these, but I wonder, is there a singular document reference that lists all the implicit conversions that happen in SQL Server?


There absolutely is!

http://msdn.microsoft.com/en-us/library/ms187928.aspx

It lists all the conversions that can happen Implicitly and those that can not.
Post #1154484
Posted Thursday, August 4, 2011 12:18 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,228, Visits: 1,046
Tom.Thomson (8/4/2011)
just yet another case where (T-)SQL gets it wrong with NULL. But then IsNumeric is such an awful function anyway that that's teh least of its worries (perhaps the biggest is its name).


IsNumeric is a function that was included into (T)SQL from other Languages that "Get It Wrong" the same way.
Very similar to STUFF
Post #1154489
Posted Thursday, August 4, 2011 12:20 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 9:36 PM
Points: 9,927, Visits: 11,189
sknox (8/4/2011)
While it's in many ways better than IsNumeric, TRY_CONVERT does have some problems.

Oddly enough, this is exactly the stance I took a week or so ago in a discussion elsewhere. The simple answer is to add a test for the item being converted being NULL.

It's difficult to add to your example, because it uses a constant, but in real-world applications we will need to be careful to distinguish between the NULL meaning convert-failure, and the NULL meaning input-was-NULL.

In the end, I was convinced that this was a non-issue, largely because the alternatives were worse (e.g. a can-I-convert-this function). I still think it will catch people out, but I am in the minority




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1154491
Posted Thursday, August 4, 2011 12:29 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,228, Visits: 1,046
SQLkiwi (8/4/2011)
sknox (8/4/2011)
While it's in many ways better than IsNumeric, TRY_CONVERT does have some problems.

Oddly enough, this is exactly the stance I took a week or so ago in a discussion elsewhere. The simple answer is to add a test for the item being converted being NULL.

It's difficult to add to your example, because it uses a constant, but in real-world applications we will need to be careful to distinguish between the NULL meaning convert-failure, and the NULL meaning input-was-NULL.

In the end, I was convinced that this was a non-issue, largely because the alternatives were worse (e.g. a can-I-convert-this function). I still think it will catch people out, but I am in the minority


I have to agree. Every time I look at things like this my brain starts repeating the following mantra:
"Code to enforce Business Rules should not be in SQL!"

Validation of user interfact data is best done by application Functions or Subroutines, not DML.
Post #1154506
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse