September 14, 2011 at 5:09 am
what is the difference between char and char(1) and which is best to use.
September 14, 2011 at 5:33 am
It's always better to specify the length. Different types have different default lenghts which are also altered be different circumstances. Which is why I don't memorise it. I just declare correctly.
September 14, 2011 at 6:55 am
declare @chr char
declare @chr1 char(1)
select LEN('1234567890abcdefghij0987654321xyz') -- = 33
-- when declared as CHAR or VARCHAR, default length of variable is 1, as shown.
set @chr = '1234567890abcdefghij0987654321xyz'
set @chr1 = '1234567890abcdefghij0987654321xyz'
select @chr
select 'LEN(@chr)', LEN(@chr) -- by default it selects first character.
select @chr1
select 'LEN(@chr1)', LEN(@chr1) -- selects the specified length = 1
-- when CHAR or VARCHAR is used in CONVERT function, default length of variable is 30, as shown.
select 'char', CONVERT(char, '1234567890abcdefghij0987654321xyz')
-- below char(1) will select specified LENGTH of 1.
select 'char(1)', CONVERT(char(1), '1234567890abcdefghij0987654321xyz')
I hope this helps.
Vasu
September 14, 2011 at 7:11 am
Hmmm... You can try these lines to see the difference:
select CONVERT(char, 'This is a SQL Server Central Forum')
select CONVERT(char(1), 'This is a SQL Server Central Forum')
select CONVERT(char(100), 'This is a SQL Server Central Forum')
select CHAR(1)
select CHAR(100)
September 14, 2011 at 7:23 am
Sudhir Dwivedi (9/14/2011)
Hmmm... You can try these lines to see the difference:select CONVERT(char, 'This is a SQL Server Central Forum')
select CONVERT(char(1), 'This is a SQL Server Central Forum')
select CONVERT(char(100), 'This is a SQL Server Central Forum')
select CHAR(1)
select CHAR(100)
How come the last select CHAR(100)
give output as d ??
plz explain
Regards,
Sushant
Regards
Sushant Kumar
MCTS,MCP
September 14, 2011 at 10:22 pm
SKYBVI (9/14/2011)
How come the last select CHAR(100)
give output as d ??
plz explain
Regards,
Sushant
Here, it is a function.
September 15, 2011 at 12:11 am
Char is a function also, returning ascii character.
Therefore, select Char(100) returns you 'd'
September 15, 2011 at 1:48 am
Many thanks for the wonderful answers.
However I would like to know when we are trying to design a table, what should be preferred and why?
September 15, 2011 at 3:32 am
Always, always, always, always specify the lengths of your columns. The defaults are not consistent and most people don't know what they are.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 15, 2011 at 5:04 am
GilaMonster (9/15/2011)
Always, always, always, always specify the lengths of your columns. The defaults are not consistent and most people don't know what they are.
Another reason why I strongly bother to NOT remember them. It forces you to never assume anything.
Moreoever if, and it's possible, MS changes those defaults then you need to know them across all versions too.
Much simpler to just type 4 more characters.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply