{"id":117,"date":"2014-05-01T12:23:00","date_gmt":"2014-05-01T11:23:00","guid":{"rendered":"https:\/\/debuggersspace.com\/index.php\/2014\/05\/01\/difference-between-char-varchar-and-nvarchar-in-sql-server\/"},"modified":"2014-05-01T12:23:00","modified_gmt":"2014-05-01T11:23:00","slug":"difference-between-char-varchar-and-nvarchar-in-sql-server","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2014\/05\/01\/difference-between-char-varchar-and-nvarchar-in-sql-server\/","title":{"rendered":"Difference between char varchar and nvarchar in sql server?"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>2 Minute, 29 Second                <\/div>\n\n            <\/div><div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<b><u><span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Char DataType<o:p><\/o:p><\/span><\/span><\/u><\/b><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Char datatype which is used to store fixed length of characters. Suppose if we declared char(50) it will allocates memory for 50 characters. Once we declare char(50) and insert only 10 characters of word then only 10 characters of memory will be used and other 40 characters of memory will be wasted.<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<\/div>\n<p><span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><a href=\"https:\/\/www.blogger.com\/null\" name=\"more\" style=\"border: 0px; color: #d1580d; line-height: 20px; margin: 0px; padding: 0px; text-decoration: underline; vertical-align: baseline;\" target=\"_blank\" rel=\"noopener\"><\/a><br style=\"color: #222222; line-height: 20px;\" \/><\/span><\/span><br \/>\n<\/p>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<b><u><span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">varchar DataType<o:p><\/o:p><\/span><\/span><\/u><\/b><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">Varchar means variable characters and it is used to store non-unicode characters. It will allocate the memory based on number characters inserted. Suppose if we declared varchar(50) it will allocates memory of 0 characters at the time of declaration. Once we declare varchar(50) and insert only 10 characters of word it will allocate memory for only 10 characters.<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<b><u><span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">nvarchar DataType<o:p><\/o:p><\/span><\/span><\/u><\/b><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\">nvarchar datatype same as&nbsp;<b>varchar<\/b>&nbsp;datatype but only difference&nbsp;<b>nvarchar<\/b>&nbsp;is used to store Unicode characters and it allows you to store multiple languages in database.&nbsp;<b>nvarchar<\/b>&nbsp;datatype will take twice as much space to store extended set of characters as required by other languages.<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><span style=\"border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\">So if we are not using other languages then it\u2019s better to use&nbsp;<b>varchar<\/b>&nbsp;datatype instead of&nbsp;<b>nvarchar<\/b><\/span><span style=\"border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><o:p><\/o:p><\/span><\/span><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><b><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><br \/><\/span><\/b><\/span><\/div>\n<div style=\"border: 0px; color: #222222; line-height: 20px; margin: 0px 0px 0.0001pt; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><\/span><\/span><\/p>\n<div style=\"color: black; line-height: 19px; margin: 15px 15px 2px;\">\n<span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><b>Non-Unicode vs. Unicode Data Types: Comparison Chart<\/b><\/span><\/span><\/div>\n<p><span style=\"background-color: white; border: 0px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><span style=\"font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;\"><\/p>\n<div style=\"color: black; line-height: 19px; margin: 15px 15px 2px;\">\nThe primary difference between unicode and non-Unicode data types is the ability of Unicode to easily handle the storage of foreign language characters which also requires more storage space.<\/div>\n<div style=\"color: black; line-height: 19px; margin: 15px 15px 2px;\">\n<\/div>\n<table border=\"1\" cellpadding=\"0\" style=\"color: black;\">\n<tbody>\n<tr>\n<td><b>Non-Unicode<\/b><\/td>\n<td><b>Unicode<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>(char, varchar, text)<\/b><\/td>\n<td><b>(nchar, nvarchar, ntext)<\/b><\/td>\n<\/tr>\n<tr>\n<td>Stores data in fixed or variable length<\/td>\n<td>Same as non-Unicode<\/td>\n<\/tr>\n<tr>\n<td><b>char<\/b>: data is padded with blanks to fill the field size. For example, if a char(10) field contains 5 characters the system will pad it with 5 blanks<\/td>\n<td><b>nchar<\/b>: same as char<\/td>\n<\/tr>\n<tr>\n<td><b>varchar<\/b>: stores actual value and does not pad with blanks<\/td>\n<td><b>nvarchar<\/b>: same as varchar<\/td>\n<\/tr>\n<tr>\n<td>requires&nbsp;<b>1 byte<\/b>&nbsp;of storage<\/td>\n<td>requires&nbsp;<b>2 bytes<\/b>&nbsp;of storage<\/td>\n<\/tr>\n<tr>\n<td><b>char<\/b>&nbsp;and&nbsp;<b>varchar<\/b>: can store up to&nbsp;<b>8000<\/b>&nbsp;characters<\/td>\n<td><b>nchar<\/b>&nbsp;and&nbsp;<b>nvarchar<\/b>: can store up to&nbsp;<b>4000<\/b>&nbsp;characters<\/td>\n<\/tr>\n<tr>\n<td>Best suited for&nbsp;<b>US English<\/b>: &#8220;One problem with data types that use 1 byte to encode each character is that the data type can only represent 256 different characters. This forces multiple encoding specifications (or code pages) for different alphabets such as European alphabets, which are relatively small. It is also impossible to handle systems such as the Japanese Kanji or Korean Hangul alphabets that have thousands of characters.&#8221;<sup>1<\/sup><\/td>\n<td>Best suited for systems that need to support at least one&nbsp;<b>foreign language<\/b>: &#8220;The Unicode specification defines a single encoding scheme for most characters widely used in businesses around the world. All computers consistently translate the bit patterns in Unicode data into characters using the single Unicode specification. This ensures that the same bit pattern is always converted to the same character on all computers. Data can be freely transferred from one database or computer to another without concern that the receiving system will translate the bit patterns into characters incorrectly.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/span><\/span><\/div>\n<div>\n<span style=\"border: 0px; font-family: &quot;verdana&quot; , sans-serif; font-size: 10pt; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;\"><b><br \/><\/b><\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Char DataType Char datatype which is used to store fixed length of characters. Suppose if we declared char(50) it will allocates memory for 50 characters. Once we declare char(50) and insert only 10 characters of word then only 10 characters of memory will be used and other 40 characters of memory will be wasted. varchar [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[22,27],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-l-t","category-sql"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Char DataType Char datatype which is used to store fixed length of characters. Suppose if we declared char(50) it will allocates memory for 50 characters. Once we declare char(50) and insert only 10 characters of word then only 10 characters of memory will be used and other 40 characters of memory will be wasted. varchar&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/117"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":0,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}