Character string data types

Character string data types are used to hold alpha numeric characters and string of alpha numeric characters.

Following data types are part of Character string data types:

Char : It’s a fixed length character data type.  

Syntax for Char data type is :

Char(N)

N = Number of character that the variable / column should hold.

N can vary from 1 to 8000

Size of the Character data type will be N bytes irrespective of whether N characters  data is accommodated or not in the variable/column.


Varchar : This is also used to hold character data. Since the size of this data type varies based on the data in the variable/column ,this data type is named as Varchar.

Syntax for Char data type is :

Varchar(N)

N = Number of character that the variable / column should hold.

N can vary from 1 to 8000

If a variable holds only M characters in VARCHAR(N) data type , then data base will allocate M bytes for that variable.  (M should be less than N).

Example:

Declare @pChar          CHAR(10)
SET @pChar               =  ‘ABCD’

Declare @pVarChar    VARCHAR(10)
SET @pVarChar   =  ‘ABCD’

In the above example @pChar uses 10bytes of space.
But, @pVarChar uses only 4bytes of space .

Text :  Char and Varchar data types can hold maximum of 8000 character only.
To hold the data which need more space than 8000 character TEXT data type can be used.

Text data type can hold data/string up to 2GB size.

Maximum number of characters allowed in TEXT data type is: 2^31-1 (2,147,483,647)
That is maximum size of TEXT data type is: 2,147,483,647

This entry was posted in . Bookmark the permalink.

Leave a reply