Casting question

  • ah ... this was soo easy, i just selected the answer in one click....:Whistling: but selected the wrong one. (interesting read, so far)

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • kapil_kk (3/15/2013)


    Danny Ocean (3/15/2013)


    Really a nice and good question.

    one more thing apart from this question. if you set ARITHABORT OFF and then execute below code, it will give output as "*" due to insufficient char length. and if you set char length more than 7, it will give "0.111111".

    It denotes that we need to provide an insufficient length whenever we convert values in char or varchar.

    SET ARITHABORT OFF

    select isnull ( convert (char(3),1.0/9) ,'*')

    Vinay, I tried with ARITHABORT OFF but still I am getting same error "Arithmetic overflow error converting numeric to data type varchar."

    set arithabort off

    select ISNULL(CONVERT(char(3),1.0/9),'*')

    Kapil, you need to SET ANSI_WARNINGS OFF. Look at below two examples. Example 1 execute successfully but example 2 give an error.

    ---- Example 1

    SET ANSI_WARNINGS OFF

    SET ARITHABORT OFF

    GO

    select isnull ( convert (char(3),1.0/9) ,'*') ,isnull ( convert (char(8),1.0/9) ,'*')

    GO

    SET ARITHABORT ON

    SET ANSI_WARNINGS ON

    ---- Example 2

    SET ANSI_WARNINGS ON

    GO

    select isnull ( convert (char(3),1.0/9) ,'*') ,isnull ( convert (char(8),1.0/9) ,'*')

    GO

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • This was removed by the editor as SPAM

  • Nice question, thanks. It made me think - I better rest now ...... ๐Ÿ˜‰

    Dave Morris :alien:

    "Measure twice, saw once"

  • Danny Ocean (3/15/2013)


    kapil_kk (3/15/2013)


    Danny Ocean (3/15/2013)


    Really a nice and good question.

    one more thing apart from this question. if you set ARITHABORT OFF and then execute below code, it will give output as "*" due to insufficient char length. and if you set char length more than 7, it will give "0.111111".

    It denotes that we need to provide an insufficient length whenever we convert values in char or varchar.

    SET ARITHABORT OFF

    select isnull ( convert (char(3),1.0/9) ,'*')

    Vinay, I tried with ARITHABORT OFF but still I am getting same error "Arithmetic overflow error converting numeric to data type varchar."

    set arithabort off

    select ISNULL(CONVERT(char(3),1.0/9),'*')

    Kapil, you need to SET ANSI_WARNINGS OFF. Look at below two examples. Example 1 execute successfully but example 2 give an error.

    ---- Example 1

    SET ANSI_WARNINGS OFF

    SET ARITHABORT OFF

    GO

    select isnull ( convert (char(3),1.0/9) ,'*') ,isnull ( convert (char(8),1.0/9) ,'*')

    GO

    SET ARITHABORT ON

    SET ANSI_WARNINGS ON

    ---- Example 2

    SET ANSI_WARNINGS ON

    GO

    select isnull ( convert (char(3),1.0/9) ,'*') ,isnull ( convert (char(8),1.0/9) ,'*')

    GO

    Yes, It works....

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks for the question!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Thanks for the question. Great Friday question to end the week!

  • Christian Buettner-167247 (3/15/2013)


    Good question but the correct answer is "0 " (with 2 trailing spaces), not "0".#

    Edit: Add example:

    SELECT REPLACE(ISNULL ( CONVERT (char(3),1/9),'*'),' ','<Space>') AS Ret

    0<Space><Space>

    That's correct. Convert to varchar to remove the trailing spaces. ๐Ÿ™‚

    --------------------------------------------------------------------------------------
    Hai Ton
    My Db4Breakfast blog.

  • okbangas (3/15/2013)


    Really easy question, but the explanation puzzles me. As far as I can see it is no implicit conversion, just a pure integer division.

    I assume it is a reference to the 1 and the 9 being interpreted as integers.

  • Good question and thanks!

    Not all gray hairs are Dinosaurs!

  • Thanks - EZPZ

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • I guess I'm the only one that initially looked for '1/9' as an answer choice. Doh.

  • Nice question, but it's a pity that the correct answer isn't any of the options. I picked '0' as it's the least wrong, but it definitely is wrong. The correct answer is '0 ' (two trailing spaces) and '0' isn't even a possible value for a char(3) object.

    Tom

  • Technically the answer is '0 ', but it was easy enough to deduce the answer choice that the author was looking for. If "None of the above" had been a choice then I imagine the comments on this thread would be much more "spirited". ๐Ÿ™‚

    Good question overall. Thanks Eli!

  • Nice question, make us thinking.

    --------------------------------------
    ;-)โ€œEverything has beauty, but not everyone sees it.โ€ โ€• Confucius

Viewing 15 posts - 16 through 30 (of 34 total)

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