SQL Server Data Types

10-AUG-2010, last improvement 15-AUG-2011
I have to store numbers from 1 through 9999. Which data type should I choose?

I have to support different versions of SQL Server. Which data types are version safe?

How much space does a data type use?

Below is one big table that answers all these questions!

SQL Server data types
PrecedenceData TypeClassRangeSpace Used7.0200020052008Remarks
*1 Using variable length columns will add some extra bytes to every row; 3 bytes for the first 8 variable length columns.
*2 Use is discouraged; a better data type is available that serves the same purpose.
18tinyintNumeric data0 - 2551 byteYYYY
17smallintNumeric data-32,768 - 32,7672 bytesYYYY
16intNumeric data-2,147,483,648 - 2,147,483,6474 bytesYYYY
14smallmoneyNumeric data-214,748.3648 - 214,748.36474 bytesYYYYAuthor recommends using decimal() instead
12decimal(9,y)Numeric data-999,999,999 - 999,999,9995 bytesYYYYPrecision up to 9 digits uses 5 bytes. Numeric is a synonym for Decimal.
13moneyNumeric data-922,337,203,685,477.5808 - 922,337,203,685,477.58078 bytesYYYYAuthor recommends using decimal() instead
15bigintNumeric data-9,223,372,036,854,775,808 - 9,223,372,036,854,775,8078 bytesYYY
12decimal(19,y)Numeric data-9,999,999,999,999,999,999 - 9,999,999,999,999,999,9999 bytesYYYYPrecision from 10 up to 19 digits uses 9 bytes
12decimal(28,y)Numeric data-10^28+1 - 10^28-113 bytesYYYYPrecision from 20 up to 28 digits uses 13 bytes
12decimal(38,y)Numeric data-10^38+1 - 10^38-117 bytesYYYYPrecision from 29 up to 38 digits uses 17 bytes
28char(x)Character data1 - 8,000 characters1 - 8000 bytesYYYYchar(1) uses 1 byte and can store 1 character
27varchar(x)Variable Length Character data0 - 8,000 characters2 - 8002 bytesYYYYvarchar(5) uses 2 bytes when empty and 7 bytes when occupied with 5 characters*1
27varchar(max)Variable Length Binary data0 - 2,147,483,645 characters2 bytes - 2 gigabytesYYvarchar(max) uses 2 bytes when empty and 3 bytes when occupied with 1 character*1
21textVariable Length Character data0 - 2,147,483,647 characters16 bytes - 2 gigabytesYYY*2Y*2text uses 16 bytes when empty and 17 bytes when occupied with 1 character
26nchar(x)Character data1 - 4,000 characters2 - 8000 bytesYYYYnchar(5) uses 10 bytes and can store 5 character
25nvarchar(x)Variable Length Character data0 - 4,000 characters2 - 8002 bytesYYYYnvarchar(5) uses 2 bytes when empty and 12 bytes when occupied with 5 characters*1
25nvarchar(max)Variable Length Binary data0 - 1,073,741,822 characters2 bytes - 2 gigabytesYYnvarchar(max) uses 2 bytes when empty and 4 bytes when occupied with 1 character*1
20ntextVariable Length Character data0 - 1,073,741,823 characters16 bytes - 2 gigabytesYYY*2Y*2ntext uses 16 bytes when empty and 18 bytes when occupied with 1 character
8dateTemporal data0001-01-01 - 9999-12-313 bytesY
9time(2)Temporal data00:00:00.00 - 23:59:59.993 bytesYPrecision up to 2 digits uses 3 bytes
7smalldatetimeTemporal data1900-01-01 00:00 - 2079-06-06 23:594 bytesYYYY
9time(4)Temporal data00:00:00.0000 - 23:59:59.99994 bytesYPrecision of 3 or 4 digits uses 4 bytes
9time(7)Temporal data00:00:00.0000000 - 23:59:59.99999995 bytesYPrecision from 5 up to 7 digits uses 5 bytes
5datetime2(2)Temporal data0001-01-01 00:00:00.00 - 9999-12-31 23:59:59.996 bytesYPrecision up to 2 digits uses 6 bytes
5datetime2(4)Temporal data0001-01-01 00:00:00.0000 - 9999-12-31 23:59:59.99997 bytesYPrecision of 3 or 4 digits uses 7 bytes
6datetimeTemporal data1753-01-01 00:00:00.000 - 9999-12-31 23:59:59.9978 bytesYYYY
5datetime2(7)Temporal data0001-01-01 00:00:00.0000000 - 9999-12-31 23:59:59.99999998 bytesYPrecision from 5 up to 7 digits uses 8 bytes
4datetimeoffset(2)Temporal data0001-01-01 00:00:00.00 - 9999-12-31 23:59:59.998 bytesYPrecision up to 2 digits uses 8 bytes
4datetimeoffset(4)Temporal data0001-01-01 00:00:00.0000 - 9999-12-31 23:59:59.99999 bytesYPrecision of 3 or 4 digits uses 9 bytes
4datetimeoffset(7)Temporal data0001-01-01 00:00:00.0000000 - 9999-12-31 23:59:59.999999910 bytesYPrecision from 5 up to 7 digits uses 10 bytes
19bitBinary data1 bit1 byteYYYYbit uses 1 byte and can store 1 bit, 8 bits use 1 byte and can collectively store 8 bits. Because of nullability, 8 bit columns require more space than 1 binary(1) column
30binary(x)Binary data8 - 64,000 bits1 - 8000 bytesYYYYAs of SQL Server 2005 binary(1) uses 1 byte and can store 8 bits. SQL Server 2000 and earlier uses 4 more bytes
29varbinary(x)Variable Length Binary data0 - 64,000 bits2 - 8002 bytesYYYYAs of SQL Server 2005 varbinary(5) uses 2 bytes when empty and 7 bytes when occupied with 33 bits or more. SQL Server 2000 and earlier uses 2 more bytes*1
29varbinary(max)Variable Length Binary data0 - 17,179,869,160 bits2 bytes - 2 gigabytesYYvarbinary(max) uses 2 bytes when empty and 3 bytes when occupied with up to 8 bits *1
22imageVariable Length Binary data0 - 2,147,483,647 bytes16 bytes - 2 gigabytesYYY*2Y*2image uses 16 bytes when empty and 17 bytes when occupied with 1 byte
24uniqueidentifierGlobally Unique Identifier1 GUID16 bytesYYYY
23timestampDatabase-wide Unique Number1 number8 bytesYYYY
11float(24)Approximate Numeric data-3.40E+38 - 3.40E+384 bytesYYYYMantissa up to 24 bits uses 4 bytes. Real is a synonym for Float(24). Author recommends never to use this data type for money amounts
10float(53)Approximate Numeric data-1.79E+308 - 1.79E+3088 bytesYYYYMantissa from 25 up to 53 bits uses 8 bytes. Author recommends never to use this data type for money amounts
3xmlXML data2 bytes - 2 gigabyesYYThe "stored representation" cannot exceed 2 gigabytes in size
2sql_variantDependsDepends16 - 8,016 bytesYYYAuthor recommends avoiding this data type
1User defined data typeUDTDependsunknownYYAuthor recommends avoiding this data type
1geography(NULL)Spatial dataempty2 bytesY*1
1geography('Point')Spatial data1 point24 bytesY*1
1geography('Line')Spatial dataLineString with 1 line40 bytesY*1
1geography('LineString')Spatial dataLineString with multiple lines82 - ? bytesYUses 34 bytes plus 16 bytes for every point in the LineString*1
1geography('Polygon')Spatial data82 - ? bytesYUses 34 bytes plus 16 bytes for every point in the Polygon*1


Back to SQL Server main menu. Mail your comments to gertjans@xs4all.nl.