i was wondering how to use IsNumeric in SQL, it’s a bit different from VBScript, but i think i was able to get around it, ie:
IF 1 = ISNUMERIC('5675754674')
BEGIN
...
END
Would this be a way around it? What I really want to do is:
IF ISNUMERIC('5675754674')
BEGIN
...
END
but that gives an error. Example 1 seems to work, but just making sure i’m doing this right, wasn’t able to find any good resources online about it.
There is no
booleanin SQL Server. This means you can’t just sayIF (expression); you must compare it to something, because it does returntrueorfalsein the same sense as you’re probably used to in other languages.Just a preference, but I would prefer to write it this way:
There is no way in SQL Server to avoid the comparison to 1, as in your second example.
Also as an aside you should be aware of the weaknesses of
ISNUMERIC()– it can give false positives for “numeric” values such as.,CHAR(9),e,$and a host of other non-numeric strings. If you want to know if something is an integer, for example, better to say:But even that is not a complete and valid test because it will return true for values
> (2^32)-1and it will return false for negative values.Another downside to
ISNUMERIC()is that it will return true if the value can be converted to any of the numeric types, which is not the same as all numeric types. Often people test forISNUMERIC()and then try to cast aFLOATto aSMALLINTand the conversion fails.In SQL Server 2012 you will have a new method called
TRY_CONVERT()which returnsNULLif the conversion to the specified data type is not valid.