1. CHAR Datatype:
It is a datatype in SQL which is used to store character string of fixed length specified. If the length of the string is less than set or fixed-length then it is padded with extra blank spaces so that its length became equal to the set length when PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled. The storage size of the CHAR datatype is n bytes(set length). We should use this datatype when we expect the data values in a column are of the same length.
Example:
Consider the Query:
CREATE TABLE Student(Name VARCHAR(30), Gender CHAR(6));
INSERT into Student VALUES('Herry', 'Male');
INSERT into Student VALUES('Mahi', 'Female');
SELECT LENGTH(Gender) FROM Student;
OUTPUT:
LENGTH(Gender)
6
6
2. VARCHAR Datatype:
It is a datatype in SQL which is used to store character string of variable length but a maximum of the set length specified. If the length of the string is less than set or fixed-length then it will store as it is without padded with extra blank spaces. The storage size of the VARCHAR datatype is equal to the actual length of the entered string in bytes. We should use this datatype when we expect the data values in a column are of variable length.
Example:
Consider the Query:
CREATE TABLE Student(Name VARCHAR(20), Gender CHAR(6));
INSERT into Student VALUES('Herry', 'Male');
INSERT into Student VALUES('Mahi', 'Female');
SELECT LENGTH(Name) FROM Student;
OUTPUT:
LENGTH(Name)
5
4
Difference between CHAR and VARCHAR datatypes:
SR.NO. | CHAR | VARCHAR |
---|
1. | CHAR datatype is used to store character strings of fixed length | VARCHAR datatype is used to store character strings of variable length |
2. | In CHAR, If the length of the string is less than set or fixed-length then it is padded with extra memory space. | In VARCHAR, If the length of the string is less than the set or fixed-length then it will store as it is without padded with extra memory spaces. |
3. | CHAR stands for "Character" | VARCHAR stands for "Variable Character" |
4. | Storage size of CHAR datatypes is equal to n bytes i.e. set length | The storage size of the VARCHAR datatype is equal to the actual length of the entered string in bytes. |
5. | We should use the CHAR datatype when we expect the data values in a column are of the same length. | We should use the VARCHAR datatype when we expect the data values in a column are of variable length. |
6. | CHAR takes 1 byte for each character | VARCHAR takes 1 byte for each character and some extra bytes for holding length information |
9. | Better performance than VARCHAR | Performance is not good as compared to CHAR |
Conclusion: VARCHAR saves space when there is variation in the length of values, but CHAR might be performance-wise better.
Similar Reads
VARCHAR, VARCHAR(MAX), and NVARCHAR in MS SQL Server In SQL Server, VARCHAR, VARCHAR(MAX) and NVARCHAR are used to store variable-length text data. VARCHAR is efficient for non-Unicode text up to 8,000 characters, while VARCHAR(MAX) handling larger text data up to 2 GB. NVARCHAR supports Unicode data, making it suitable for multilingual applications.
3 min read
SQL Wildcard Characters SQL wildcard characters are powerful tools that enable advanced pattern matching in string data. They are especially useful when working with the LIKE and NOT LIKE operators, allowing for efficient searches based on partial matches or specific patterns. By using SQL wildcard characters, we can great
6 min read
SQL Server CHARINDEX() function CHARINDEX() function in SQL Server returns the position of a substring within a given string. The search performed in this function is case-insensitive. SyntaxCHARINDEX function syntax is: CHARINDEX(substring, string, [starting_position] ParametersCHARINDEX function accepts 3 parameters: substring:
2 min read
Difference between char, varchar and VARCHAR2 in Oracle 1. CHAR :The char data type is used to store the character values. It is a fixed-length data type i.e. once initialized we cannot change the size at execution time. Hence, it is also called a Static datatype.It is used to store normal characters and alphanumeric characters too. The char data type ca
5 min read
NCHAR() Function in SQL Server NCHAR() function : This function in SQL Server is used to return the Unicode character that is based on the number code. For example, if the specified number is 65 then this function will return A. Features : This function is used to find the Unicode character of a given number. This function accept
2 min read