DEV Community

Cover image for Difference between varchar, nvarchar in sql server ?
twostepdeveloper
twostepdeveloper

Posted on • Edited on

nvarchar vs varchar Difference between varchar, nvarchar in sql server ?

In this article i will differentiate between varchar, nvarchar data type.

About the Varchar:

  1. It is a variable that has a length data type.
  2. It is use to store non-Unicode characters.
  3. It Occupy 1 byte of space for each character.

example :
DECLARE @name VARCHAR(20)

SET @name = 'Rizwan'

SELECT @name AS 'Name', DATALENGTH(@name) AS 'Occupied Length' ,
LEN(@name) AS 'Name Length'
Result : Name Occupied Length Length
Rizwan 6(6*1 byte=6bytes) 6

Alt Text

About the NVARCHAR data:

  1. It is a variable that has length data type.
  2. It is use to store Unicode characters.
  3. It Occupy 2 bytes of space for each character.

example :
DECLARE @name NVARCHAR(20)

SET @name = 'Rizwan'

SELECT @name AS 'Name', DATALENGTH(@name) AS 'Occupied Length' ,
LEN(@name) AS 'Name Length'
Result : Name Occupied Length Length
Rizwan 12(6*2 byte=12 bytes) 6

Alt Text

Conclusion

This tiny article is just to make you aware of the basic differences between the VARCHAR and NVARCHAR data types they are used to store characters, numbers or special characters.
This is my first article
I hope you all will like and that will be helpful to you most of developers confuse about varchar ,nvarchar when we should use varchar,nvarchar.
Please Leave your comments about your opinion good or bad.
Sharing is caring :)

Top comments (1)

Collapse
 
twostepdeveloper profile image
twostepdeveloper

Comment if you think it is useful post