confusion with unicode length nchar(1)

  • Hello Team,

    BOL says that NCHAR ie unicode character is 2 byte long so that it can store around 65000+ characters. Then how come the below code works well when we give nchar(1)?

    DECLARE @nstring nchar(1)

    SET @nstring = N'Åkergatan 24'

    SELECT UNICODE(@nstring), NCHAR(UNICODE(@nstring))

    -LK

  • nchar(1) allows SQL Server to store 1 unicode character.

    A byte is 8 bits long (allowing 256 different combinations). Unicode allows you to store 65000 different combinations, which takes 2 bytes of storage (16 bits). Therefore, although you are storing a single unicode character, it actually takes 2 bytes on disk.

  • Do you mean nchar(1) ---> 1 doesn't signifie one byte but one unicode character.

    -lk

  • luckysql.kinda (9/11/2009)


    Do you mean nchar(1) ---> 1 doesn't signifie one byte but one unicode character.

    -lk

    Yes

  • To expand the original example to illustrate the point:

    DECLARE @nstring nchar(1)

    SET @nstring = N'Åkergatan 24'

    SELECT UNICODE(@nstring),

    NCHAR(UNICODE(@nstring)),

    len_function = LEN(@nstring),

    datalength_function = DATALENGTH(@nstring);

    While I'm here, I'm also goint to mention that the LEN function also doesn't count trailing spaces (it counts leading spaces). DATALENGTH returns the storage size, regardless of padding.

    Top marks for posting code to make your question clear, and for using the correct N prefix for unicode.

    Paul

  • Yes Paul. This helped.

    -lk

Viewing 6 posts - 1 through 5 (of 5 total)

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