SQL Data Types

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.

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 TypesDescriptionLength
intStores the integer values ranging from -2,147,483,648 to 2,147,483,6474 Bytes
tinyintStores the integer values ranging from 0 to 2551 Byte
smallintAccept integer values ranging from -32,768 to 32,7672 Byte
bigintIt stores the integer values ranging from -253 to -253 – 18 Bytes
moneyIt stores the financial values ranging from -922,337,203,685,477.5808 to -922,337,203,685,477.58078 Bytes
smallmoneyIt stores financial values ranging from -214,748.3648 to 214,748.36474 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
realThe 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.

DataTypesDescription & 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

DataTypesDescriptionLength
bitIt stores a single bit of data1 byte for an 8-bit column
binary(n)It stores n bytes of binaryn Bytes, where the range of n is from 1 to 8000
varbinary(n)It stores approximately n bytes of binary infoActual 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 infoActual string length + 2 Byte

SQL Date and Time Data Types

DataTypesDescriptionLengthExample
dateIt stores dates between January 01, 0001, and December 31, 99993 Bytes2016-08-19
DateTimeIt stores date and time between January 01, 1753, and December 31, 99998 Bytes2016-08-19 10:15:45.0110
datetime2It stores date and time between January 01, 0001, and December 31, 99996 – 82016-08-19 10:15:45.0110112
datetimeoffsetIt is the same as datetime2, and in addition, it will include the Universal Time Coordinate (UTC) offset (we also called as GMT)8 – 10 Bytes2016-08-19 10:15:45.0110112 +05:30
smalldatetimeIt stores the date and time between January 01, 1990, and June 06, 20794 Bytes2016-08-19
timeIt stores time with an accuracy of 100 nanoseconds. The time range is between 00:00:00.0000000, and 23:59:59.99999993 – 5 Bytes10: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.

Categories SQL