• er.mayankshukla (8/26/2014)


    er.mayankshukla (8/26/2014)Hello Experts,

    I have defined a Nvarchar column of 30 bytes

    Lets say

    create table demo (name nvarchar(30))

    But when I see the length of this column by selecting table and pressing Alt+F1

    Or using col_length

    I see length of 60 but it should be 62

    However,

    Nvarchar(n) length is 2n+2

    Varchar(n) length is 2n

    Also,

    How it works internally?

    I mean if Varchar(30) then sql assigns 60 bytes of data but I fill only 20 bytes so doest it takes back 40 bytes of data after column is filled ?

    Could you kindly answer this part as well

    nvarchar would contain up to 60 bytes of data (representing 30 characters). But SQL will only use the data bytes it actually needs. For example, if you stored only 5 characters in the column, SQL would only use 10 bytes. The 2-byte overhead is always used, of course, even if the string is NULL or empty.

    If you used nchar (rather than nvarchar) then SQL would use reserve all 60 bytes no matter how many bytes of data you actually put into the column. Even empty columns would require 60 bytes of data storage.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.