What are the differences between char, varchar, nchar and nvarchar?

Senan Mehdiyev
2 min readJun 20, 2022

--

As it is known, today the acquisition of information from data, the use of this information in business and finding solutions to problems is increasing day by day. But at the same time, as the volume of data increases, it is important to protect data, store it efficiently, and ensure high-speed data transfer. Therefore, the information is mainly stored in the database. MS SQL is one of the most widely used DBMS(Database Management Systems) and can store data in tables and in the columns that make it up. When creating these columns, it is necessary to select the data types that correspond to the situation and the information to be stored. Among them, I will talk about char, varchar, nchar and nvarchar, which are often confused and asked about the differences.

These datatypes have different characteristics. The main difference between varchar and char is that varchar depends on the value we enter, i.e the character size of that value is plus 2 bytes (to save the size of the value). Char also takes up as much space as we set, regardless of the value. For example; If we enter “Salam” in the column with varchar (10), it will be 7 bytes (for the size of 5 bytes-characters, to keep the size of 2 bytes-value), and in the column with char (10) will be 10 bytes, regardless of the size of the characters. It should be noted that although varchar char is more convenient to save space, it is slower in terms of performance. For this reason, you need to decide according to the situation, Space? or Speed ?. The difference between nchar and char is n (National Language Character Set). Char can only store certain characters that are not located inside Unicode, but nchar Unicode can store a wide range of Chinese, Japanese, Korean, Greek and other letters and symbols that match the character type. However, nchar has twice the size of the symbol. Therefore, it is necessary to use it when needed and save space!

Scheme of memory location according to data types
Scheme of memory location according to data types

If you liked the article or have any ideas, we will be glad if you like the post and comment. Thanks!

--

--

Senan Mehdiyev
Senan Mehdiyev

Written by Senan Mehdiyev

Self-motivated, ambitious to work, research, and develop in Data Science/ML/DL, have communicative skills, highly inspired to run with a competitive team

No responses yet