none
Differrence between Char vs Varchar and Nchar vs NVarchar

    Question

  • Hi guys, i want to know to exact difference between and use of

    1.Char (vs) Varchar

    2.NChar (vs) NVarchar.

    I have surfed,but i could not get the exact solution.

    Regards

    @Anil Pomar@

    Thursday, April 25, 2013 5:12 AM

Answers

  • Hi Anil,

    Answers to this questions are basic, and I will speak in SQL Server perspective because this is not C# question, although varchar, char, nvarchar and nchar are not resreved only for SQL Server (many RDBMS uses same or similar types - like for example MySQL). 

    1. Char vs Varchar - these are types that can store non unicode strings, and each character (or empty space) in variables of this types consumes 1 byte of memory. Difference is that varchar is variable length string and char is fixed length string. So when you declare variable of varchar(50) type and you store string "Test" in it it will acctualy allocate only 4 bytes of memory. If you would do that in variable of type char(50) it will allocate 50 bytes of memory because it is fixed length string.

    2. Nchar vs Nvarchar - Same as above but it can store unicode charachters, so you can in one column have multiple languages and letters (latin, cyrilic, arab etc). Because it is unicode each charachter consumes 2 bytes of memory. Nchar is fixed length and nvarchar is variable lenght - same as above.

    When to use it in SQL Server ?

    You should choose nvarchar or nchar (unicode support) if there is any possability that you will put unicode letters in that column, that is easy. Choice between fixed length strings and variable length strings in sql server can affect performanse in reading and writing. So if you expect many writes to columns (table) then better performance can be with fixed length strings because there will be less fragmentation and page splits. If you are expecting mostly reads from columns (table) then better is to use variable length strings because you will have to read less data from disk and disk IO will be lower.

    That is in short, but I am sure that you can find a lot more on MSDN or just google it.


    if (helpful) then Vote();

    Thursday, April 25, 2013 5:29 AM
  • Hi Mr Anil

    diff b/w char vs varchar is

    char is fixed length memory(not changable once declared)

    varchar is variable length memory storage( changable)

    ex:

    char n(100);

    n=N;

    it occupites 1 char length

    remaing 99 in empty not possible to use

    for varchar we can mange the legth


    • Marked as answer by Anil Pomar Thursday, April 25, 2013 6:40 AM
    • Unmarked as answer by Anil Pomar Thursday, April 25, 2013 6:40 AM
    • Marked as answer by Anil Pomar Wednesday, May 15, 2013 6:19 AM
    Thursday, April 25, 2013 5:38 AM
  • Hi,

    First of all this question is related to SQl server, not C#.

    CHAR should be used for storing fix length character strings. If this type is used to store varibale length strings, it will waste a lot of disk space.
    VARCHAR is used to store variable length character strings. The string value's length will be stored on disk with the value itself.But, remember CHAR is faster than VARCHAR - some times up to 50% faster

    NCHAR should be used for storing fix length unicode character strings. If this type is used to store varibale length strings, it will waste a lot of disk space.
    VARCHAR is used to store variable length unicode character strings. The string value's length will be stored on disk with the value itself.

    And for your information unicode characters are used when you require the use of extended character sets(mostly special symbol) which are not available in non-unicode character set.

    Hope this may help.


    One good question is equivalent to ten best answers.

    Thursday, April 25, 2013 5:38 AM

All replies

  • Hi Anil,

    Answers to this questions are basic, and I will speak in SQL Server perspective because this is not C# question, although varchar, char, nvarchar and nchar are not resreved only for SQL Server (many RDBMS uses same or similar types - like for example MySQL). 

    1. Char vs Varchar - these are types that can store non unicode strings, and each character (or empty space) in variables of this types consumes 1 byte of memory. Difference is that varchar is variable length string and char is fixed length string. So when you declare variable of varchar(50) type and you store string "Test" in it it will acctualy allocate only 4 bytes of memory. If you would do that in variable of type char(50) it will allocate 50 bytes of memory because it is fixed length string.

    2. Nchar vs Nvarchar - Same as above but it can store unicode charachters, so you can in one column have multiple languages and letters (latin, cyrilic, arab etc). Because it is unicode each charachter consumes 2 bytes of memory. Nchar is fixed length and nvarchar is variable lenght - same as above.

    When to use it in SQL Server ?

    You should choose nvarchar or nchar (unicode support) if there is any possability that you will put unicode letters in that column, that is easy. Choice between fixed length strings and variable length strings in sql server can affect performanse in reading and writing. So if you expect many writes to columns (table) then better performance can be with fixed length strings because there will be less fragmentation and page splits. If you are expecting mostly reads from columns (table) then better is to use variable length strings because you will have to read less data from disk and disk IO will be lower.

    That is in short, but I am sure that you can find a lot more on MSDN or just google it.


    if (helpful) then Vote();

    Thursday, April 25, 2013 5:29 AM
  • Hi Mr Anil

    diff b/w char vs varchar is

    char is fixed length memory(not changable once declared)

    varchar is variable length memory storage( changable)

    ex:

    char n(100);

    n=N;

    it occupites 1 char length

    remaing 99 in empty not possible to use

    for varchar we can mange the legth


    • Marked as answer by Anil Pomar Thursday, April 25, 2013 6:40 AM
    • Unmarked as answer by Anil Pomar Thursday, April 25, 2013 6:40 AM
    • Marked as answer by Anil Pomar Wednesday, May 15, 2013 6:19 AM
    Thursday, April 25, 2013 5:38 AM
  • Hi,

    First of all this question is related to SQl server, not C#.

    CHAR should be used for storing fix length character strings. If this type is used to store varibale length strings, it will waste a lot of disk space.
    VARCHAR is used to store variable length character strings. The string value's length will be stored on disk with the value itself.But, remember CHAR is faster than VARCHAR - some times up to 50% faster

    NCHAR should be used for storing fix length unicode character strings. If this type is used to store varibale length strings, it will waste a lot of disk space.
    VARCHAR is used to store variable length unicode character strings. The string value's length will be stored on disk with the value itself.

    And for your information unicode characters are used when you require the use of extended character sets(mostly special symbol) which are not available in non-unicode character set.

    Hope this may help.


    One good question is equivalent to ten best answers.

    Thursday, April 25, 2013 5:38 AM