In SQL Server, every column, local variable, expression, and parameter is associated with a specific data type. This data type is crucial as it defines the kind of data the object can store, whether it’s integer values, text, monetary figures, dates and times, or binary data. Understanding data types is fundamental for efficient database design and management in SQL Server.
SQL Server offers a rich set of system data types to accommodate various data storage needs. Beyond these built-in types, you have the flexibility to create custom data types using Transact-SQL or the .NET Framework. Alias data types, for instance, are user-defined types based on the system-provided data types, allowing for semantic clarity and reusability. For more advanced customization, user-defined types can be built leveraging the methods and operators of classes created in .NET Framework programming languages.
When operations involve expressions with differing data types, SQL Server has rules to determine the resulting data type, collation, precision, scale, or length, ensuring data integrity and predictable outcomes.
For compatibility with ISO standards, SQL Server also provides data type synonyms. Furthermore, specific versions like Azure Synapse Analytics and Microsoft Fabric have their own nuances in data type implementation, detailed in their respective documentations.
Data Type Categories in SQL Server
SQL Server data types are broadly categorized based on the kind of data they represent and how they are stored. Here’s a breakdown of the main categories:
In SQL Server, some data types are classified based on their storage behavior, particularly concerning large amounts of data:
- Large value data types: These are designed to handle substantial text or binary data, including
varchar(max)
andnvarchar(max)
. - Large object data types: These are legacy types and newer alternatives for very large data, encompassing
text
,ntext
,image
,varbinary(max)
, andxml
.
[!NOTE]
The system stored proceduresp_help
returns-1
as the length for large-value andxml
data types, indicating their ability to store data beyond typical length limitations.
Exact Numerics
Exact numeric data types are used for storing numbers with precision and scale defined. These are ideal for financial or scientific data where accuracy is paramount.
Data type | Description |
---|---|
bigint | Integers in the range of -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807). |
int | Integers in the range of -2,147,483,648 through 2,147,483,647. |
smallint | Integers in the range of -32,768 through 32,767. |
tinyint | Integers in the range of 0 through 255. |
bit 1 | Integer value of either 0, 1, or NULL. |
decimal 2 | Fixed precision and scale numeric data from -10^38 +1 through 10^38 -1. |
numeric 2 | Fixed precision and scale numeric data from -10^38 +1 through 10^38 -1. |
money | Monetary values from -922,337,203,685,477.5808 to 922,337,203,685,477.5807. |
smallmoney | Monetary values from -214,748.3648 to 214,748.3647. |
Alt Text: Diagram illustrating the hierarchy and relationships among exact numeric SQL data types like bigint, int, smallint, tinyint, bit, decimal, numeric, money, and smallmoney, emphasizing their precision and storage capacity.
1 The bit data type is primarily used for storing Boolean values, representing true/false or yes/no conditions.
2 The decimal and numeric data types are functionally identical, offering the same level of precision and scale control.
Approximate Numerics
Approximate numeric data types are used for floating-point numbers. While they do not store exact values, they are suitable for scientific computations and applications where a degree of approximation is acceptable in exchange for a wider range and performance benefits.
Data type | Description |
---|---|
float | Floating precision number data from -1.79E+308 through 1.79E+308. |
real | Floating precision number data from -3.40E+38 through 3.40E+38. |
Date and Time
Date and time data types are essential for tracking temporal information. SQL Server provides a variety of date and time types to suit different levels of precision and time zone requirements.
Data type | Description |
---|---|
date | Date only, ranging from January 1, 0001 through December 31, 9999. |
datetime | Date and time values from January 1, 1753, through December 31, 9999, with an accuracy of 3.33 milliseconds. |
datetime2 | Date and time values from January 1, 0001 through December 31, 9999, with an accuracy of 100 nanoseconds. |
datetimeoffset | Date and time values with time zone awareness. |
smalldatetime | Date and time values from January 1, 1900, through June 6, 2079, with an accuracy of one minute. |
time | Time of day, based on a 24-hour clock, with an accuracy of 100 nanoseconds. |
Alt Text: Visual representation outlining various date and time SQL data types such as date, datetime, datetime2, datetimeoffset, smalldatetime, and time, highlighting their temporal ranges, precision levels, and time zone capabilities.
Character Strings
Character string data types are used to store text-based data. SQL Server offers both fixed-length and variable-length character string types.
Data type | Description |
---|---|
char | Fixed-length, non-Unicode character data with a maximum length of 8,000 characters. |
varchar | Variable-length, non-Unicode character data with a maximum length of 8,000 characters. |
varchar(max) | Variable-length, non-Unicode character data with a maximum length of 2^31-1 bytes. |
text | Variable-length, non-Unicode character data with a maximum length of 2^31-1 bytes. |
Unicode Character Strings
Unicode character string data types are designed to store characters from all languages. They are crucial for applications requiring multilingual support.
Data type | Description |
---|---|
nchar | Fixed-length, Unicode character data with a maximum length of 4,000 characters. |
nvarchar | Variable-length, Unicode character data with a maximum length of 4,000 characters. |
nvarchar(max) | Variable-length, Unicode character data with a maximum length of 2^31-1 bytes. |
ntext | Variable-length, Unicode character data with a maximum length of 2^30-1 characters. |
Alt Text: Comparative chart displaying text and character SQL data types like char, varchar, text, nchar, nvarchar, and ntext, contrasting their fixed or variable length nature, Unicode support, and maximum storage capacities.
Binary Strings
Binary string data types are used to store raw binary data, such as images, documents, or any other data in binary format.
Data type | Description |
---|---|
binary | Fixed-length binary data with a maximum length of 8,000 bytes. |
varbinary | Variable-length binary data with a maximum length of 8,000 bytes. |
varbinary(max) | Variable-length binary data with a maximum length of 2^31-1 bytes. |
image | Variable-length binary data with a maximum length of 2^31-1 bytes. |
Other Data Types
SQL Server also includes specialized data types for specific needs.
Data type | Description |
---|---|
cursor | A reference to a database cursor. |
hierarchyid | A variable-length, system data type representing a position in a hierarchy. |
sql_variant | Stores values of various SQL Server-supported data types, except text, ntext, image, timestamp, and sql_variant. |
table | A special data type used to store a result set for later processing. |
timestamp | Row versioning data type, automatically updated on every insert or update. |
uniqueidentifier | A globally unique identifier (GUID). |
xml | Stores XML format data. |
geography 1 | Stores geographics data, like GPS locations. |
geometry 1 | Stores geometric data, like shapes and spatial relationships. |
Alt Text: Informative table presenting miscellaneous SQL data types such as cursor, hierarchyid, sql_variant, table, timestamp, uniqueidentifier, xml, geography, and geometry, detailing their specific functionalities and applications within database systems.
1 The geography and geometry data types are spatial types, enabling the storage and analysis of spatial data within SQL Server.
Understanding these data types is crucial for effective database design, ensuring data integrity, and optimizing query performance in SQL Server. Choosing the right data type for each column based on the nature of the data is a key aspect of database development best practices.
Related Content
Data type synonyms
CREATE TYPE
Table data types in Synapse SQL
Data type
sp_help