The SQL data type is an attribute that specifies the type of data an object can hold. For instance, character or string data, binary information, integers, financial data, date and time, and so on. The following datatypes are special SQL data types to store geographical data.
- Geometry Data type: It is used to represent the data in a two-dimensional Euclidean coordinate system
- Geography Datatype: The functionality of this datatype is the same as the Geometry. The only difference between both these datatypes is, when you declare the variable with Geography data type, you are specifying the points in terms of latitude and longitude.
SQL Data Types Available List
The following are the available list of SQL server Data Types and their description.
SQL Numeric Data Types
The following are the available list of numerical data types in SQL Server
SQL Data Types | Description | Length |
---|---|---|
int | Stores the integer values ranging from -2,147,483,648 to 2,147,483,647 | 4 Bytes |
tinyint | Stores the integer values ranging from 0 to 255 | 1 Byte |
smallint | Accept integer values ranging from -32,768 to 32,767 | 2 Byte |
bigint | It stores the integer values ranging from -253 to -253 – 1 | 8 Bytes |
money | It stores the financial values ranging from -922,337,203,685,477.5808 to -922,337,203,685,477.5807 | 8 Bytes |
smallmoney | It stores the financial values ranging from -214,748.3648 to 214,748.3647 | 4 Bytes |
decimal(p,s) | It stores the decimal values. Here p is the Precision, s is the Scale, and the maximum precision value is 38 digits. | 5 – 17 Bytes |
numeric(p,s) | Functionality of this datatype is equivalent to decimal (p, s). | 5 – 17 Bytes |
float(n) | It stores the floating point values with precision of 7 digit (if n = 24, and 15 digit (if n = 53). | 5 – 17 Bytes. If n = 24 it uses 4, and If n = 53 it uses 8 |
real | Functionality of this datatype is equivalent to float(24). | 4 Bytes |
SQL Character and String Data Types
These SQL data types stores n Unicode characters 2n Bytes, where the range of n is from 1 to 4000.
DataTypes | Description & Length |
---|---|
char(n) | It stores n characters Bytes, where the range of n is from 1 to 8000. |
nchar(n) | It stores approximately n characters Actual string length + 2 Byte, where the range of n is from 1 to 8000 |
varchar(n) | It stores up to 231-1 characters. Length = Actual string length + 2 Byte |
varchar(max) | It stores approximately n characters2n(Actual string length) + 2 Byte, where the range of n is from 1 to 8000 |
nvarchar(n) | It stores up to ((231-1) /2) – 2 characters |
nvarchar(max) | It stores up to ((2n-1) /2) – 2 characters. 2n(Actual string length) + 2 Byte |
SQL Binary Data Types
DataTypes | Description | Length |
---|---|---|
bit | It stores a single bit of data | 1 byte for 8-bit column |
binary(n) | It stores n bytes of binary | n Bytes, where the range of n is from 1 to 8000 |
varbinary(n) | It stores approximately n bytes of binary info | Actual string length + 2 Byte, where the range of n is from 1 to 8000 |
varbinary(max) | It stores up to 231-1 bytes of binary info | Actual string length + 2 Byte |
SQL Date and Time Data Types
DataTypes | Description | Length | Example |
---|---|---|---|
date | It stores dates between January 01, 0001, and December 31, 9999 | 3 Bytes | 2016-08-19 |
DateTime | It stores date and time between January 01, 1753, and December 31, 9999 | 8 Bytes | 2016-08-19 10:15:45.0110 |
datetime2 | It stores date and time between January 01, 0001, and December 31, 9999 | 6 – 8 | 2016-08-19 10:15:45.0110112 |
datetimeoffset | It is same as datetime2, and in addition, it will include the Universal Time Coordinate (UTC) offset (we also called as GMT) | 8 – 10 Bytes | 2016-08-19 10:15:45.0110112 +05:30 |
smalldatetime | It stores date and time between January 01, 1990, and June 06, 2079 | 4 Bytes | 2016-08-19 |
time | It stores time with an accuracy of 100 nanoSeconds. The time range is between 00:00:00.0000000, and 23:59:59.9999999 | 3 – 5 Bytes | 10:15:45.0110136 |
Difference between Char and Varchar, Nvarchar data types in SQL Server
The following section explains the difference with an example of each SQL Server data type.
Char DataType
Character or Char data type in Sql Server is used to store the fixed length of characters.
For example, if we declare variable, @str CHAR (50) means the Server will allocate memory for 50 characters. Suppose we inserted 10 characters into a @str variable, 10 characters of memory used, and the remaining 40 characters will waste.
Varchar
A Varchar data type in Sql Server means variable character, and it is used to store the non-Unicode characters. Varchar allocates the memory based on the number of characters inserted into it.
For example, if we declare variable, @name VARCHAR (50) means, it will allocate memory for 0 characters. Suppose we inserted 10 characters into a @name variable. Then, this SQL data type will allocate the memory to store those 10 characters (it allocate literally 10 characters and no wastage). If you think that the size of the column might exceed 8000 bytes, please use Varchar(max).
NVarchar DataType
NVarchar data type in Sql Server is the same as the Varchar datatype. The only difference between them is, it is used to store Unicode characters. NVarchar datatype will take double (2n) space to store the other language characters. If you think that the size of the column might exceed 4000 bytes, please use nvarchar(max).
NOTE: When you are not storing other language characters, then I suggest you choose SQL Server Varchar data type instead of char or NVarchar.