Auto Ads code

Saturday, June 8, 2019

varchar VS nvarchar

What are the difference between VARCHAR AND NVARCHAR in sql server?


  • Defination

    • VARCHAR is non-unicode character type with variable length
    • NVARCHAR is unicode character type with variable length

  • Size

    • VARCHAR occupies 1 byte per character.
      • for example: DECLARE @NAME VARCHAR(10)='JAYENDRA'
      • occupies 8 bytes
    • NVARCHAR occupies 2 byte per character.
      • for example: DECLARE @NAME NVARCHAR(10)='JAYENDRA'
      • occupies 16 bytes

  • Usage

    • use VARCHAR type when your column have only non-unicode character data only like any character which has ascii value 0 to 255
    • use NVARCHAR type when you table column contains unicode character data like any other language character ex. gujarati or hindi

  • Storage size

    • Occupies number of bytes equal to the number of Characters entered and 2 bytes extra for defining offset.
    • Occupies number of bytes equal to the number of Characters entered * 2 (twice of character) and 2 bytes extra for defining offset.

No comments:

Post a Comment