July 10, 2009 at 8:29 am
Comments posted to this topic are about the item int & char
July 11, 2009 at 12:34 am
I really don't understand why result query is '*'.
34 is ASCII value for " and I thought it would cast int to char ?
July 13, 2009 at 3:13 am
I also don't get it. Why doesn't it cast 34 to char(34) (double quotes ?)
Anyone care to enlighten us?
July 13, 2009 at 3:17 am
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!
July 16, 2009 at 12:53 pm
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)
July 17, 2009 at 3:14 am
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
July 18, 2009 at 4:04 am
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
July 21, 2009 at 4:06 am
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)
July 27, 2009 at 5:39 am
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.
November 23, 2009 at 11:50 pm
Was tricky one. Some knowledge gained.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply