SQL TEXT Vs VARCHAR Datatype

When you talk about character kinds, you're referring to the types of characters that you can store in your database. In PostgreSQL, there are three character types.

The first is character(n), often known as char(n), which is used to store characters with a defined length.

The other is the character varying(n), often known as varchar (n), where n is the amount of characters that make up a character.

The final character type is text, which can be any length. It's worth noting that when using char(n) and varchar (n), n should be a positive integer value indicating the column's length.


SQL TEXT Datatype

The TEXT datatype is used to hold character values and has no limit on length because there is no n specified.

The value of n in varchar is optional; if it is not supplied, the column's length becomes infinite, and it behaves similarly to the text datatype. When n, i.e. length, is not mentioned in the declaration, the performance of varchar(n) and text is the same.

Example: Let’s create a new table(say,text) for the demonstration using the below commands:

CREATE TABLE text(
    id no PRIMARY KEY,
    a TEXT,
        b TEXT
);

SQL VARCHAR Datatype

VARCHAR is a character data type supported by PostgreSQL.

This data type is used to store characters that are only a certain length.

In PostgreSQL, it is represented as varchar (n), where n is the maximum character length.

If n is not specified, the type is varchar, which has an unlimited length.

Any attempt to store a longer string in a column defined with varchar(n) results in an error from PostgreSQL.

However, if the extra characters are all spaces, PostgreSQL will truncate the spaces to the maximum length and store the string.

The only advantage of specifying the length specifier for the varchar data type is that PostgreSQL will check for and report an error if you attempt to insert a longer string.

Example: Let’s create a new table(say, test) for the demonstration using the below commands:

CREATE TABLE test (
    id no PRIMARY KEY,
    a VARCHAR (1),
        b VARCHAR(10)
);

SQL Difference Between TEXT And VARCHAR Datatype

The distinguish in text & varchar datatype points are tabulated in below table:-

TEXT VARCHAR
Each character takes up one byte. It uses one byte for each character.
Since SQL Server 6.5, it has been present. SQL Server 2005 is the first to include it.
Even after execution and storage, the padding and spaces are preserved. When padding and spaces are specified, they are truncated during execution.
Has a limitless length already. There isn't a way to specify the value for n. When the value of n is not specified, it is assumed to be infinity.
A length-checking cycle is not performed. Query performs an extra cycle to check the length of the value being stored.
This data type can hold an unlimited number of NON UNICODE characters by defining it as TEXT. The maximum storage capacity is 231-1 bytes.(two gigabytes) The maximum number of NON UNICODE characters for this data type is unlimited because it is defined as NVARCHAR (MAX). MAX denotes the maximum storage size of 231-1 bytes (2 GB). The storage size is equal to the length of the data entered plus 2 bytes.
Variable length values are stored as Strings. A variable length value is stored as a String. DECLARE @VarChar varchar = 'VarChar' PRINT DATALENGTH(@VarChar) OUTPUT: 1
If an index is required, it cannot be created on the TEXT data type. When we create an index on this data type, it throws the exception error. Even though it is required, indexes cannot be created on VARCHAR(MAX) data types. When we create an index on this data type, it throws the exception error.
Additional functionality = and GROUP BY can’t be used in TEXT data type. Additional functionality = and GROUP BY can be used in VARCHAR(MAX) data type.
The SQL server will use the value of the TEXT data type that is stored in a separate LOB (Large Object) data page that is stored outside of the row. The row in the data table will only contain a 16-byte pointer to the LOB data pages where the actual data is stored. It does not support UNICODE characters and stores content in variable sizes. SQL server will store the value of VARCHAR(MAX) in a row unless it exceeds 8000 bytes/characters, in which case it will store the value outside of the row. i.e. it uses standard data pages until the content fills 8k of data. When it overflows, the data is stored as an old TEXT Data type, and a pointer replaces the old content. It does not support UNICODE characters and stores content in variable sizes.
When the content (data) size is variable, there are no UNICODE characters and/or NULL, and the content length exceeds 8000 bytes/characters, use the TEXT data type. When the content (data) size is variable, there are no UNICODE characters and/or NULL, and the content length exceeds 8000 bytes/characters, use VARCHAR(MAX).
The TEXT data type has no optional parameter values. When the optional parameter value MAX [VARCHAR(MAX)] is not specified in the variable declaration or column definition, it is assumed to be 1.
To read, write, or update TEXT data, use the SELECT, INSERT, or UPDATE statements, as well as special statements like READTEXT, WRITETEXT, and UPDATETEXT. You can directly read, write or update VARCHAR(MAX) data using the SELECT, INSERT orUPDATE statements no special statements READTEXT, WRITETEXT and UPDATETEXT can be used.
Performance VARCHAR(MAX) is a data type that replaces the TEXT data type. The LIKE statement is the same for both data types. Microsoft recommends using VARCHAR(MAX) instead of Text data type for storing large amounts of data in a single column.