int & char

  • Comments posted to this topic are about the item int & char

  • I really don't understand why result query is '*'.

    34 is ASCII value for " and I thought it would cast int to char ?

  • I also don't get it. Why doesn't it cast 34 to char(34) (double quotes ?)

    Anyone care to enlighten us?

  • mike (7/13/2009)


    I also don't get it. Why doesn't it cast 34 to char(34) (double quotes ?)

    Anyone care to enlighten us?

    OK I get it. Should really read BOL before posting!

  • mike (7/13/2009)


    mike (7/13/2009)


    I also don't get it. Why doesn't it cast 34 to char(34) (double quotes ?)

    Anyone care to enlighten us?

    OK I get it. Should really read BOL before posting!

    Please enlighten us! Don't feel like reading BOL now.

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • For those too lazy to look up CAST and CONVERT (in the truncating and rounding results section):

    The integer value 34 is not converted to char(34), it is converted to '34'. (This will no doubt surprise .NET programmers.)

    Since the string '34' is two characters long and the column is defined as char(1), the value stored in the char(1) column is '*' to show that it wouldn't fit.

    Interestingly, the truncation behaviour differs between char and nchar - with char you get asterisks, with nchar you get an error...

    select convert(char(2), convert(int, 34)) -- '34'

    select convert(char(1), convert(int, 34)) -- '*'

    select convert(nchar(2), convert(int, 34)) -- N'34'

    select convert(nchar(1), convert(int, 34)) -- Error 8115

    Paul

  • Yes Paul, that's right. I'm reading all these posts now and I find them very interessting.

    Thank you for the good explanations.

    Richellere

  • Paul White (7/17/2009)


    For those too lazy to look up CAST and CONVERT (in the truncating and rounding results section):

    The integer value 34 is not converted to char(34), it is converted to '34'. (This will no doubt surprise .NET programmers.)

    Since the string '34' is two characters long and the column is defined as char(1), the value stored in the char(1) column is '*' to show that it wouldn't fit.

    Interestingly, the truncation behaviour differs between char and nchar - with char you get asterisks, with nchar you get an error...

    select convert(char(2), convert(int, 34)) -- '34'

    select convert(char(1), convert(int, 34)) -- '*'

    select convert(nchar(2), convert(int, 34)) -- N'34'

    select convert(nchar(1), convert(int, 34)) -- Error 8115

    Paul

    Thanks Paul,

    Good way of explaining it. Oh and by the way, I am not normally lazy.

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • I've got a few of these questions wrong now but this one really surprised me. Whilst I never would have imagined that it would convert an INT datatype value of 34 to a CHAR(34) datatype I was expecting that it would convert it to varchar or text and then truncate. I should have been alerted by the fact that my original answer of "4" was not an option, since I thought it might do the equivalent of a RIGHT(column,1) truncate, so went for the equivalent of LEFT(column,1)

    I use CONVERT(CHAR(8),@variable) for loads of things where I want to truncate because I'm all for speed and think that LEFT(CONVERT(VARCHAR(8000),@variable),8) is slower. I'll have to watch for this sort of thing if I ever want to truncate a column of data directly in a table.

  • Was tricky one. Some knowledge gained.



    [font="System"]Bhavesh Patel[/font]

    http://bhaveshgpatel.wordpress.com/

Viewing 10 posts - 1 through 9 (of 9 total)

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