|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 1:53 PM
Points: 785,
Visits: 1,534
|
|
| Essentially, the reason why it doesn't work is because an integer is a subset of the numeric datatypes, and not vice-versa. Things which are numeric are not necessarily integers, while all integers are numeric.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Tuesday, April 02, 2013 3:48 AM
Points: 660,
Visits: 557
|
|
I agree with the above post, integer is very specific, in fact forget the odd character, even select convert(int,'0.0') will not work. select convert(decimal(2,1),'0,.0') will give the error that was supplied as one of the answers to the question.
Interestingly (or not, it is Friday lunch time), money to int will work no worries, rounding to the nearest int, so select convert(int,convert(money,'0,.0')) will work fine if you're worried about isnumeric letting you down!
-------- I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Friday, March 15, 2013 2:43 PM
Points: 3,924,
Visits: 1,554
|
|
Christian Buettner (8/7/2009)
If I am not totally wrong, the behaviour is not due to IsNumeric itself, but to the fact that you can cast this value to money or smallmoney. Try this: SET NOCOUNT ON SELECT CONVERT(money ,'1,0.2') A, CONVERT(money ,'1.0,2') B, CONVERT(money ,'1,100,00.2') C A.......B......C.......... 10.2000 1.0200 110000.2000
Now dont ask me why...
Very interesting. Never thought or tried it or say never came across this kind of issue. Not to you Chris, but to some one else who could give better explanation.
My question - Why ?
SQL DBA.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 1:53 PM
Points: 785,
Visits: 1,534
|
|
It's just the way that SQL Server deals with Monetary data. It's always got four trailing spaces for decimals, and the decimal symbol (".") is interpreted as being the symbol to indicate that you're dealing with the decimal part of the Money value, while the comma (",") is treated as being part of the non-decimal part.
EG: 1,000 is the same as 1000, while 1.000 is the same as 1, and 1,000.1 is the same as 1000.1
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 9:52 AM
Points: 2,796,
Visits: 1,125
|
|
isnumeric is just evaluating valid characters.
This will produce 1 also
select isnumeric('-$,')
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 2:34 PM
Points: 565,
Visits: 360
|
|
ronmoses (8/7/2009)
Dang it, I knew that was the answer and didn't pick it. That seemed like the obvious "otherwise, why would I ask the question?" answer. But no, I had to go and think about it. Stupid me.  Ditto . That said, I would love to hear from the people who thought the answer for a CONVERT(int, {anything}) would be either 0.0 or 0,.0!
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 10:31 PM
Points: 339,
Visits: 950
|
|
I'd like to seee the explanation expanded to explain that select convert(money,'0,.0') would work, and that is why isNumeric is returning true (i.e. that isnumeric is ignoring the comma because that's what convert does for money).
Try:
select convert(money,'0,,,0,.0,,,0')
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 5:31 AM
Points: 2,226,
Visits: 438
|
|
I encountered this issue a couple of month ago and i solved it with the following udf from ASPFAQ.com [url=http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html][/url]
CREATE FUNCTION dbo.isReallyNumeric ( @num VARCHAR(64) ) RETURNS BIT BEGIN IF LEFT(@num, 1) = '-' SET @num = SUBSTRING(@num, 2, LEN(@num)) DECLARE @pos TINYINT SET @pos = 1 + LEN(@num) - CHARINDEX('.', REVERSE(@num)) RETURN CASE WHEN PATINDEX('%[^0-9.-]%', @num) = 0 AND @num NOT IN ('.', '-', '+', '^') AND LEN(@num)>0 AND @num NOT LIKE '%-%' AND ( ((@pos = LEN(@num)+1) OR @pos = CHARINDEX('.', @num)) ) THEN 1 ELSE 0 END END GO CREATE FUNCTION dbo.isReallyInteger ( @num VARCHAR(64) ) RETURNS BIT BEGIN IF LEFT(@num, 1) = '-' SET @num = SUBSTRING(@num, 2, LEN(@num)) RETURN CASE WHEN PATINDEX('%[^0-9-]%', @num) = 0 AND CHARINDEX('-', @num) <= 1 AND @num NOT IN ('.', '-', '+', '^') AND LEN(@num)>0 AND @num NOT LIKE '%-%' THEN 1 ELSE 0 END END
/Håkan Winther MCITP:Database Developer 2008
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 6:12 AM
Points: 2,526,
Visits: 3,620
|
|
kevin.l.williams (8/7/2009)
isnumeric is just evaluating valid characters. This will produce 1 also select isnumeric('-$,') Hi Kevin,
I think that is not true. Try select isnumeric('$$') for example. It will evaluate to false, although this character ($) was no issue in your previous example.
The reason why your example is returning true, is probably the fact that it can be cast to (small)money.
Try finding an example that is not convertible to a numeric datatype but that returns true in this function.
Best Regards,
Chris Büttner
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 1:53 PM
Points: 785,
Visits: 1,534
|
|
Try finding an example that is not convertible to a numeric datatype but that returns true in this function.
That's impossible :P as per the definition of the function, ISNUMERIC only returns true if the input is convertible to one of the numeric data types.
|
|
|
|