August 7, 2009 at 12:21 am
Comments posted to this topic are about the item Conversion Fun
August 7, 2009 at 2:00 am
But aren't you trying to convert it to an integer in the code you supplied rather than a numeric data type?
Granted you'll still get the error but if you run the code you'll get the response "Conversion failed when converting the varchar value '0,.0' to data type int" which isn't one of the available answers.
August 7, 2009 at 2:54 am
..And answer is in the subject itself...................
August 7, 2009 at 4:28 am
just 3 days back i had a similar issue with isnumeric. If this QOTD had come then it would have been helpful.
"Keep Trying"
August 7, 2009 at 4:37 am
why isnumeric gives true when the content contains ','. May be question seems simple but i am new to sql server.
August 7, 2009 at 4:42 am
I suspect this is due to the way it handles foreign numbers. When I work with countries using format '0,0' I want it to treat it as numeric based on the server regional settings. Unfortunately, it is more likely that isnumeric() is taking naughty shortcuts and replacing certain characters like the comma - regardless of server settings - whereas convert() is not.
August 7, 2009 at 5:37 am
srikanth_pallerla (8/7/2009)
why isnumeric gives true when the content contains ','. May be question seems simple but i am new to sql server.
As per the Books Online page (http://msdn.microsoft.com/en-us/library/ms186272.aspx):
"ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0."
Valid numeric data types include things like Money data types - "ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols, see Using Monetary Data."
Integer is a pretty specific data type. It won't accept things that are considered Numeric in some cases. If you need the ability to detect whether something is an Integer, I'd suggest writing your own UDF. There's many different ways to do it. One thing you can do is to cast the value to a VARCHAR data type, then loop through every character, and see if every character evaluates to an ASCII value between 48 and 57 (numbers 0-9); if any don't,then you'll know that it's not an Integer.
August 7, 2009 at 5:47 am
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
[font="Courier New"]A.......B......C..........
10.20001.0200110000.2000
[/font]
Now dont ask me why...
Best Regards,
Chris Büttner
August 7, 2009 at 5:50 am
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. 🙂
-----
a haiku...
NULL is not zero
NULL is not an empty string
NULL is the unknown
August 7, 2009 at 6:28 am
I don't believe that isnumeric looks at the string as a whole, but as the individual characters in the string. I think it's a rather broad assertion that the string COULD be converted to a number of some fashion (integer, real, money, etc.).
LinkedIn: https://www.linkedin.com/in/sqlrv
Website: https://www.sqlrv.com
August 7, 2009 at 6:30 am
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.
August 7, 2009 at 7:10 am
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! :hehe:
--------
[font="Tahoma"]I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams[/font]
August 7, 2009 at 8:38 am
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
[font="Courier New"]A.......B......C..........
10.20001.0200110000.2000
[/font]
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.
August 7, 2009 at 9:19 am
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
August 7, 2009 at 10:07 am
isnumeric is just evaluating valid characters.
This will produce 1 also
select isnumeric('-$,')
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply