Hello all, today, I am going to percentage an interesting SQL Server interview inquiry which volition non exclusively attention y'all inward your interview but every bit good inward your day-to-day work. It explains 1 of the key concepts of SQL Server, the difference betwixt VARCHAR too CHAR information type. I am sure, y'all all conduct maintain used both of them numerous fourth dimension but because it's hence mutual many of us ignore the divergence betwixt them too when asked to select betwixt VARCHAR too CHAR on interviews, they neglect to laissez passer a convincing reason. The divergence is non only of import from interview signal of persuasion but every bit good from a robust database blueprint because an wrong alternative of information type non exclusively confine what form of information y'all tin set on but every bit good waste matter precious infinite too makes your query slower, especially when y'all conduct maintain to bargain amongst the massive sum of data.
In guild to sympathise the difference, it's every bit good of import to sympathise the similarity betwixt them so, let's get-go amongst that. VARCHAR too CHAR both stores character, text, or String information similar name, address, etc. One of the of import exceptional to know hither is that both stores non-Unicode characters too in that place is a carve upward information type NCHAR too NVARCHAR for storing Unicode characters.
The key divergence betwixt CHAR too VARCHAR is that the old is a fixed-length information type spell afterward is a variable length information type. Yes, the VAR stands for variable length inward VARCHAR. To laissez passer y'all an example, CHAR(10) is a fixed-length non-Unicode string of length 10 spell VARCHAR(10) is a variable-length non-Unicode string amongst a maximum length of 10.
This agency the actual length volition depend upon the data. For example, if y'all are going to sore a unmarried grapheme string similar "Y" too hence VARCHAR volition conduct maintain less infinite than CHAR because it volition conform depending upon the length of data. Typically, y'all would piece of occupation the char if all information values are 10 characters too varchar if the lengths vary
It's amend to piece of occupation the information type that volition conduct maintain less space. In SQL Server 2005, 2008, 2012 too 2014, NVARCHAR takes to a greater extent than infinite than VARCHAR information type, almost 2x every bit much infinite every bit VARCHAR.
So, piece of occupation VARCHAR if y'all know that all your information would endure inward ASCII encoding, but if y'all are going to shop Unicode string similar storing information from dissimilar languages too hence y'all demand to piece of occupation NVARCHAR to back upward Unicode data. NVARCHAR is must if y'all recall to back upward internationalization (i18n). You tin further Microsoft SQL for Beginners to acquire to a greater extent than close char, varchar, nchar, too nvarchar information type inward SQL
1) Both stores non-Unicode characters.
2) Both are grapheme information types.
3) Both conduct maintain 1 byte to shop 1 character.
4) The maximum length of both CHAR too VARCHAR information type is 8000 characters inward SQL Server. Maximum length is defined inward parenthesis e.g. maximum length of CHAR(6) is six grapheme too the maximum length of VARCHAR(6) is every bit good six character.
The size (9000) given to the type 'varchar' exceeds the maximum allowed for whatsoever information type (8000).
DECLARE @abc AS CHAR (8000);
DECLARE @abcd AS VARCHAR (8000);
Don't confuse length too size here, the length hither represents how many characters a CHAR or VARCHAR variable tin take, too size represents the storage bytes they conduct maintain to shop those characters. See answer)What is the divergence betwixt UNION too UNION ALL inward SQL? (answer) The divergence betwixt TRUNCATE too DELETE inward SQL? (answer) The divergence betwixt self too equi-join inward SQL? (answer) Top v SQL too Database Course for Programmers (courses) The divergence betwixt WHERE too HAVING clause inward SQL? (answer) The divergence betwixt LEFT too RIGHT OUTER JOIN inward SQL? (answer) How to notice duplicate records inward a table? (query) Difference betwixt isNull() too Collasce() inward SQL Server? (answer) 10 SQL Queries from Programming Job Interviews (queries) 5 Courses to Learn SQL Server too T-SQL (Courses)
Thanks for reading this article hence far. If y'all similar this SQL Interview inquiry too my explanation too hence delight percentage amongst your friends too colleagues. If y'all conduct maintain whatsoever questions or feedback too hence delight driblet a note.
P.S. - If y'all demand to a greater extent than SQL Interview Question for practice, y'all tin every bit good banking concern agree out 200+ SQL Interview Questions online course on Udemy which non exclusively enjoin y'all the mutual SQL questions but every bit good explicate the reply inward exceptional to fill upward gaps inward your learning.
In guild to sympathise the difference, it's every bit good of import to sympathise the similarity betwixt them so, let's get-go amongst that. VARCHAR too CHAR both stores character, text, or String information similar name, address, etc. One of the of import exceptional to know hither is that both stores non-Unicode characters too in that place is a carve upward information type NCHAR too NVARCHAR for storing Unicode characters.
The key divergence betwixt CHAR too VARCHAR is that the old is a fixed-length information type spell afterward is a variable length information type. Yes, the VAR stands for variable length inward VARCHAR. To laissez passer y'all an example, CHAR(10) is a fixed-length non-Unicode string of length 10 spell VARCHAR(10) is a variable-length non-Unicode string amongst a maximum length of 10.
This agency the actual length volition depend upon the data. For example, if y'all are going to sore a unmarried grapheme string similar "Y" too hence VARCHAR volition conduct maintain less infinite than CHAR because it volition conform depending upon the length of data. Typically, y'all would piece of occupation the char if all information values are 10 characters too varchar if the lengths vary
It's amend to piece of occupation the information type that volition conduct maintain less space. In SQL Server 2005, 2008, 2012 too 2014, NVARCHAR takes to a greater extent than infinite than VARCHAR information type, almost 2x every bit much infinite every bit VARCHAR.
So, piece of occupation VARCHAR if y'all know that all your information would endure inward ASCII encoding, but if y'all are going to shop Unicode string similar storing information from dissimilar languages too hence y'all demand to piece of occupation NVARCHAR to back upward Unicode data. NVARCHAR is must if y'all recall to back upward internationalization (i18n). You tin further Microsoft SQL for Beginners to acquire to a greater extent than close char, varchar, nchar, too nvarchar information type inward SQL
Similarities betwixt CHAR vs VARCHAR inward SQL
Now, that y'all sympathise the telephone substitution similarity too divergence betwixt char too varchar, let's encounter about to a greater extent than of import points for revision:1) Both stores non-Unicode characters.
2) Both are grapheme information types.
3) Both conduct maintain 1 byte to shop 1 character.
4) The maximum length of both CHAR too VARCHAR information type is 8000 characters inward SQL Server. Maximum length is defined inward parenthesis e.g. maximum length of CHAR(6) is six grapheme too the maximum length of VARCHAR(6) is every bit good six character.
The size (9000) given to the type 'varchar' exceeds the maximum allowed for whatsoever information type (8000).
DECLARE @abc AS CHAR (8000);
DECLARE @abcd AS VARCHAR (8000);
Don't confuse length too size here, the length hither represents how many characters a CHAR or VARCHAR variable tin take, too size represents the storage bytes they conduct maintain to shop those characters. See answer)
Thanks for reading this article hence far. If y'all similar this SQL Interview inquiry too my explanation too hence delight percentage amongst your friends too colleagues. If y'all conduct maintain whatsoever questions or feedback too hence delight driblet a note.
P.S. - If y'all demand to a greater extent than SQL Interview Question for practice, y'all tin every bit good banking concern agree out 200+ SQL Interview Questions online course on Udemy which non exclusively enjoin y'all the mutual SQL questions but every bit good explicate the reply inward exceptional to fill upward gaps inward your learning.
No comments:
Post a Comment