June 4, 2009 at 9:13 pm
Comments posted to this topic are about the item one character of data
June 4, 2009 at 9:39 pm
i think the ans is varchar(1).
Tanx 😀
June 5, 2009 at 2:05 am
Since it wont be empty or null Char(1) is the right answer to this question.
"Keep Trying"
June 5, 2009 at 2:18 am
I've chosen for nchar(1) because it only says that the value won't be NULL or empty, but there's no limitation on the characters used. Also depending on your collation sometimes a character is covered by char but in another collation it's a nchar.
Try this:
CREATE TABLE #Chars(
c1 char(1) COLLATE SQL_Latin1_General_Cp850_BIN,
c2 nchar(1)COLLATE SQL_Latin1_General_Cp850_BIN,
c3 char(1)COLLATE SQL_Latin1_General_CP1_CI_AS,
c4 nchar(1)COLLATE SQL_Latin1_General_CP1_CI_AS)
INSERT INTO #chars
VALUES(CHAR(0128),CHAR(0128),CHAR(0128),CHAR(0128))
SELECT * FROM #chars
DROP TABLE #chars
As you can see the nchar value is consistent, while the char value depends on the collation.
[font="Verdana"]Markus Bohse[/font]
June 5, 2009 at 2:54 am
I agree with MarkusB. I chose nchar(1). There's nothing in the question to suggest that this single character of data will be limited to single-byte chars. You set yourself up for trouble later if you don't anticipate all potential data.
June 5, 2009 at 3:00 am
"Either char(1) or varchar(1) may be used. Both would allocate 1 byte of space which would be used. "
doesn't varchar take extra 2 bytes for variant length? I think char(1) allocate 1 byte while varchar(1) allocate 3 bytes.
June 5, 2009 at 3:10 am
Yes, varchar(1) is definitly wrong.
Also the question wasn't which datatype uses the least space, but what is best.
But how do you define best?
[font="Verdana"]Markus Bohse[/font]
June 5, 2009 at 3:10 am
surely nchar(1)?
June 5, 2009 at 3:22 am
I was thinking beyond US-ASCII, so I chose nchar(1) 🙂
June 5, 2009 at 3:34 am
The question is open to interpretatation, but i have to agree that is should be nchar(1)
June 5, 2009 at 4:08 am
To be able to store every possible character in this variable it definitely has to be a nchar(1).
E.g. using 2-byte Unicode characters.
Leo
June 5, 2009 at 4:17 am
I agree with many others that nchar(1) should be the correct answer because I am under the impression that varchars store an additional 2 bytes of data in the background to indicate the length of the data - so therefore Varchar(1) uses 3 bytes if it contains a byte of data and 2 bytes if the field was null or empty. I remember having discussions about the sensible minimum field length for varchar fields and when chars should be used instead to store least data. I could be wrong though...
June 5, 2009 at 4:35 am
June 5, 2009 at 4:37 am
Hi,
The Online Manuals state it quite clearly:
http://msdn.microsoft.com/en-us/library/ms176089.aspx:
char [ ( n ) ]
Fixed-length, non-Unicode character data with a length of n bytes. n must be a value from 1 through 8,000. The storage size is n bytes. The ISO synonym for char is character.
varchar [ ( n | max ) ]
Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. The ISO synonyms for varchar are char varying or character varying.
http://msdn.microsoft.com/en-us/library/ms186939.aspx:
nchar [ ( n ) ]
Fixed-length Unicode character data of n characters. n must be a value from 1 through 4,000. The storage size is two times n bytes. The ISO synonyms for nchar are national char and national character.
nvarchar [ ( n | max ) ]
Variable-length Unicode character data. n can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size, in bytes, is two times the number of characters entered + 2 bytes. The data entered can be 0 characters in length. The ISO synonyms for nvarchar are national char varying and national character varying.
So, depending on whether your horizon goes beyond the US or not, you may argue either for CHAR(1) or NCHAR(1), respectively.
Best regards,
Dietmar Weickert.
June 5, 2009 at 4:55 am
As what the characters might be was not specified, add a vote for nchar(1)
but so long as question generates debate...................
---------------------------------------------------------------------
Viewing 15 posts - 1 through 15 (of 182 total)
You must be logged in to reply to this topic. Login to reply