Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sital
Yak Posting Veteran
89 Posts |
Posted - 2008-12-19 : 06:35:03
|
| Hi allWhat is the difference betweenchar(4) and varchar(4)?Both the data types stores 4 characters.When to use a varchar and char datatype?And what is the difference between varchar and nvarchar? When should both be used? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-19 : 06:37:48
|
http://www.mssqlcity.com/FAQ/General/char_vs_varchar.htm:The char is a fixed-length character data type, the varchar is a variable-length character data type.Because char is a fixed-length data type, the storage size of the char value is equal to the maximum size for this column. Because varchar is a variable-length data type, the storage size of the varchar value is the actual length of the data entered, not the maximum size for this column.You can use char when the data entries in a column are expected to be the same size.You can use varchar when the data entries in a column are expected to vary considerably in size.Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
soorajtnpki
Posting Yak Master
231 Posts |
Posted - 2008-12-19 : 07:13:45
|
| ok then why cant we use varchar always? Is there any advantage of using char? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-19 : 07:18:57
|
http://www.felgall.com/db01.htm:Why not always use VARCHAR then if it can save space when the content is shorter than the maximum size? The answer to that is that a VARCHAR has an overhead in that it needs to use an extra character (or more) to track the length of each specific field. This means that for any given data a VARCHAR field will take up more space in the database than a CHAR field just big enough to hold the data.The choice between the two is therefore dependent on the expected content for the field. If all of the data for your field will always be the exact same length then a CHAR is the obvious choice since that saves you the overhead of using VARCHAR That does not mean that using VARCHAR is necessarily the best choice if the content varies in length.Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|