The SQL data type is an attribute that specifies the type of data an object can hold. For instance, characters or strings, binary information, integers, financial data, date and time, etc. The following are special SQL data types to store geographical data.
- Geometry 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 these SQL datatypes is that when you declare the variable with the Geography data type, you specify the points in terms of latitude and longitude.
Table of contents
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.
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 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) | The functionality of this is equivalent to decimal (p, s). | 5 – 17 Bytes |
float(n) | It stores the floating point values with a precision of 7 digits (if n = 24, and 15 digits (if n = 53). | 5 – 17 Bytes. If n = 24 it uses 4, and If n = 53 it uses 8 |
real | The functionality of this datatype is equivalent to float(24). | 4 Bytes |
SQL Character and String Data Types
These SQL data types store 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 an 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 the 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 the 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 data type.
SQL Char DataType
Character or Char data type in Sql Server stores the fixed length of characters.
For example, if we declare a 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 will be used, and the remaining 40 characters will waste.
SQL Varchar Data Type
A Varchar data type in Sql Server means variable character and 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 a variable, @name VARCHAR (50) means, it will allocate memory for 0 characters. Suppose we inserted 10 characters into a @name variable. Then, this SQL Server data type will allocate the memory to store those 10 characters (it allocates 10 characters and has no wastage). If you think the column size might exceed 8000 bytes, please use Varchar(max).
SQL NVarchar Data Type
NVarchar data type in Sql Server is the same as the Varchar datatype. The only difference between them is that it is used to store Unicode characters. NVarchar datatype will take double (2n) space to store the other language characters. If you think the column size might exceed 4000 bytes, please use nvarchar(max).
NOTE: When you are not storing other language characters, then I suggest you choose Varchar data type instead of char or NVarchar.