Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between datatype char(n),varchar(n),nchar(n),nvarchar(n) in sql server?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 455
    Comment on it

    Difference between datatype char(n),varchar(n),nchar(n),nvarchar(n) in sql server?

    char(n)

    A char datatype takes 1 byte per character. It is a fixed length character datatype i.e even though character entered are less than n,fixed memory size i.e n bytes are allocated to those characters.

    For e.g: DECLARE @Name AS CHAR(50) =SURAJ. The memory allocated to NAME variable will be 50 bytes inspite of the fact that the variable contains 6 characters requiring only 6 bytes. This results in wastage of space and right side blanks are padded with empty spaces. It takes into account Non-Unicode characters i.e only supports US English.

    Optional Parameter n value can be from 1 to 8000 i.e it can store maximum 8000 Non-Unicode characters. If n is not specified then it is considered as 1.

    varchar(n)

    It is a variable length character datatype i.e memory size depends on number of characters entered. Fixed memory size is not allocated to entered characters thus saving space and n specifies the maximum characters that can be entered. Varchar type character also takes 1 byte.

    For e.g: DECLARE @Name AS VARCHAR(50) =SURAJ.The memory allocated to NAME variable will be 5 bytes plus two bytes extra for defining offset not 50 bytes which is the maximum size. It takes into account Non-Unicode characters i.e only supports US English.

    Optional Parameter n value can be from 1 to 8000 i.e it can store maximum 8000 Non-Unicode characters. If n is not specified then it is considered as 1.

    nchar(n)

    A nchar datatype takes 2 bytes per character. It is a fixed length character datatype i.e even though character entered are less than n,fixed memory size i.e n bytes are allocated to those characters resulting in wastage of memory space. NCHAR datatype needs to support atleast one foreign language like german,chinese etc.

    For e.g: DECLARE @Name AS CHAR(50) =SURAJ'. The memory allocated to NAME variable will be 50 bytes inspite of the fact that the variable contains 6 characters requiring only 12 bytes since 2 bytes per character.

    Optional Parameter n value can be from 1 to 4000 i.e maximum 4000 Unicode/Non-Unicode characters. If n is not specified then it is considered as 2.

    nvarchar(n)

    It is a variable length character datatype i.e memory size depends on number of characters entered. Fixed memory size is not allocated to entered characters thus saving space. NVARCHAR type character takes 2 byte.

    For e.g: DECLARE @Name AS VARCHAR(50) =SURAJ. The memory allocated to NAME variable will be 10 bytes plus two bytes extra for defining offset not 50 bytes which is the maximum size. It can store Unicode as well as non-Unicode data like Japnese,Korean etc.

    Optional Parameter n value can be from 1 to 4000 i.e maximum 4000 Unicode/Non-Unicode characters.If n is not specified then it is considered as 2.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: