Difference between char and char(1)

  • what is the difference between char and char(1) and which is best to use.

  • 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.

  • 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

  • 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)

  • 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

  • SKYBVI (9/14/2011)


    How come the last select CHAR(100)

    give output as d ??

    plz explain

    Regards,

    Sushant

    Here, it is a function.

  • Char is a function also, returning ascii character.

    Therefore, select Char(100) returns you 'd'

  • 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?

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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