Conversion Fun

  • SuperDBA-207096

    SSCrazy Eights

    Points: 8176

    Comments posted to this topic are about the item Conversion Fun

  • SQLPhil

    SSCarpal Tunnel

    Points: 4204

    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.

  • Joy Smith San

    SSC-Insane

    Points: 24877

    ..And answer is in the subject itself...................

  • ChiragNS

    One Orange Chip

    Points: 26137

    just 3 days back i had a similar issue with isnumeric. If this QOTD had come then it would have been helpful.

    "Keep Trying"

  • srikanth_pallerla-1082904

    Grasshopper

    Points: 23

    why isnumeric gives true when the content contains ','. May be question seems simple but i am new to sql server.

  • Todd Heidesch

    SSC Journeyman

    Points: 90

    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.

  • kramaswamy

    SSCoach

    Points: 18135

    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.

  • Christian Buettner-167247

    SSChampion

    Points: 13729

    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.2000 1.0200 110000.2000

    [/font]

    Now dont ask me why...

    Best Regards,

    Chris Büttner

  • ronmoses@gmail.com

    SSCarpal Tunnel

    Points: 4480

    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

  • Aaron N. Cutshall

    SSCrazy Eights

    Points: 8725

    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.).

  • kramaswamy

    SSCoach

    Points: 18135

    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.

  • Rob Goddard

    SSCrazy

    Points: 2572

    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]

  • SanjayAttray

    SSChampion

    Points: 13157

    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.2000 1.0200 110000.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.

  • kramaswamy

    SSCoach

    Points: 18135

    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

  • kevin.l.williams

    SSCarpal Tunnel

    Points: 4916

    isnumeric is just evaluating valid characters.

    This will produce 1 also

    select isnumeric('-$,')

Viewing 15 posts - 1 through 15 (of 25 total)

You must be logged in to reply to this topic. Login to reply