text, ntext, binary, varbinary and varbinary(max) in MS SQL Server
Last Updated :
04 Sep, 2023
Character (char) and variable character (varchar) are used in storing the fixed length of words. These data types are used for creating databases on a small scale. Suppose we have an enterprise that has various products. The database has to store product details including its description. We have char and varchar but will they be able to store paragraphs of words? NO. For such instances, the 'text' datatype is used.
Text Datatypes
The text has the capacity to store from 1 byte to 4 Gigabytes of data. We need to specify length in char and varchar but in the case of text, we do not need to specify length. Yet text works slower than char and varchar. There are several categories in Text Datatypes:
- TINY TEXT
- TEXT
- MEDIUM TEXT
- LONG TEXT
- NTEXT
TINY TEXT
The non-Unicode is the character string datatype that stores data up to 255 characters. For example, the following statement creates a column called stu_name with a length of 255 characters and we also use TEXT for the large number of characters.
CREATE TABLE GeeksforGeeks (
id INT NOT NULL AUTO_INCREMENT,
stu_name TINYTEXT NOT NULL
);
TEXT
It is a non-Unicode, character string datatype storing around 64KB of data. For example, the following statement creates a column called stu_name with a length of 65,535 characters:
CREATE TABLE GeeksforGeeks (
id INT NOT NULL AUTO_INCREMENT,
stu_name TEXT NOT NULL
);
MEDIUM TEXT
It stores up to 16MB of data. We can write description-length data with medium text.
LONG TEXT
We can store up to 4GB of data using this non-unicode datatype. Using this data type, we can type data to the length of an article.
NTEXT
A Unicode data type that stores the data without having to specify length. Storage size is double the size that is specified in the column. There are no subcategories for next.
Binary Datatypes
In a few cases, we might need to store files, and images in the database. To store this type of data, there is a datatype named binary that can store this kind of data. It has subtypes that help to store related data according to storage size. Depending on the requirements of users, we can use a variable or fixed length. These are of several types:
- BINARY
- VARBINARY
- VARBINARY(MAX)
BINARY
Binary is fixed-length data type that stores pictures, files, and other media. Storage size depends on the length specified. It can store up to 8000 bytes. For example, the following statement creates a column called image with a length of 2000 bytes so the query is shown given below.
CREATE TABLE GeeksforGeeks (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
image1 BINARY(2000)
);
VARBINARY
The name as it suggests, stores variable-length data. Storage depends upon number of bytes specified. For example, the following statement creates a column called files with a variable length so the query is shown below.
CREATE TABLE GeeksforGeeks (
id INT NOT NULL,
name VARCHAR(255) NOT NULL,
files VARBINARY(255)
);
VARBINARY(MAX)
It stores the maximum size of up to 2GB. A variable-length datatype, varbinary(max) can be used for media that require large capacity.
Conclusion
Finally, we need to talk about binary and textual data types. In the MYSQL server, both datatypes are significant. If we need to store big amounts of data, we use binary files such as LONGTEXT, LONGBLOB, etc.; whereas, for smaller amounts of data, we can use TINYTEXT, TEXT, MEDIUMTEXT, etc.
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
Numeric and Date-time data types in SQL Server MS SQL Server supports a wide range of data types. There are a few more important data types that are included in the article. In this article, we will cover numeric SQL server data types and different date-time data types. Letâs discuss one by one. bit : A bit is the smallest unit of a computer sys
3 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
Storing a Non-English String in Table â Unicode Strings in SQL SERVER In this article, we will discuss the overview of Storing a Non-English String in Table, Unicode Strings in SQL SERVER with the help of an example in which will see how you can store the values in different languages and then finally will conclude the conclusion as follows. Introduction : SQL Server
2 min read
Remove All Spaces From a String in SQL Server There are scenarios of the occurrence of spaces before and after a string and we may need to remove/trim the spaces for our use. Let us see how it is getting handled in SQL Server. Till SQL Server 2016, we have the functions called SQL LTRIM and SQL RTRIM functions. The name itself implies that LTRI
3 min read