T-SQL Data Types
This section provides detailed information about the Transact-SQL (T-SQL) data types available in Microsoft SQL Server. Understanding data types is crucial for designing efficient and accurate databases and writing effective queries.
Overview
Data types define the kind of data that a column, variable, expression, or parameter can hold. SQL Server provides a rich set of data types that cover a wide range of data storage needs.
Categories of Data Types
Exact Numerics
These data types store exact numeric values. They are used for financial calculations, identifiers, and other scenarios where precision is critical.
Data Type | Description | Storage |
---|---|---|
BIGINT |
Large integer data. | 8 Bytes |
DECIMAL / NUMERIC |
Fixed-precision and scale numbers. | Variable (up to 17-34 Bytes) |
INT |
Standard integer data. | 4 Bytes |
SMALLINT |
Small integer data. | 2 Bytes |
TINYINT |
Very small integer data. | 1 Byte |
MONEY |
Monetary values. | 8 Bytes |
SMALLMONEY |
Small monetary values. | 4 Bytes |
Approximate Numerics
These data types store approximate numeric values. They are suitable for scientific calculations where a high degree of precision is not required.
Data Type | Description | Storage |
---|---|---|
FLOAT |
Approximate floating-point numbers. | 4 or 8 Bytes |
REAL |
Approximate floating-point number (single-precision). | 4 Bytes |
Date and Time
These data types store date and time information.
Data Type | Description | Storage |
---|---|---|
DATE |
Stores a date. | 3 Bytes |
TIME |
Stores a time. | 3-5 Bytes |
DATETIME |
Stores a date and time. | 8 Bytes |
DATETIME2 |
Extended accuracy date and time. | 6-8 Bytes |
SMALLDATETIME |
Smaller date and time range. | 4 Bytes |
DATETIMEOFFSET |
Date and time with time zone offset. | 8-10 Bytes |
Character Strings
These data types store text data.
Data Type | Description | Storage |
---|---|---|
CHAR |
Fixed-length non-Unicode character data. | N Bytes (where N is the length) |
VARCHAR |
Variable-length non-Unicode character data. | N + 2 Bytes |
NCHAR |
Fixed-length Unicode character data. | 2N Bytes |
NVARCHAR |
Variable-length Unicode character data. | 2N + 2 Bytes |
Binary Strings
These data types store binary data.
Data Type | Description | Storage |
---|---|---|
BINARY |
Fixed-length binary data. | N Bytes |
VARBINARY |
Variable-length binary data. | N + 2 Bytes |
Large Object (LOB) Data Types
These data types are designed to store large amounts of data, such as text documents, images, or other large binary objects.
Data Type | Description |
---|---|
VARCHAR(MAX) |
Variable-length non-Unicode character data, up to 2^31-1 characters. |
NVARCHAR(MAX) |
Variable-length Unicode character data, up to 2^31-1 characters. |
VARBINARY(MAX) |
Variable-length binary data, up to 2^31-1 bytes. |
TEXT |
Variable-length non-Unicode character data (deprecated). |
NTEXT |
Variable-length Unicode character data (deprecated). |
IMAGE |
Binary data (deprecated). |
Other Data Types
UNIQUEIDENTIFIER
: Stores a globally unique identifier (GUID).XML
: Stores XML data.SQL_VARIANT
: Stores values of various .NET Framework data types.GEOMETRY
: Stores geometric data.GEOGRAPHY
: Stores geographic data.HIERARCHYID
: Stores hierarchical data.
Choosing the Right Data Type
Selecting the appropriate data type is essential for:
- Data Integrity: Ensures that only valid data is stored.
- Storage Efficiency: Minimizes disk space usage.
- Performance: Optimizes query processing and data manipulation.
- Application Compatibility: Ensures seamless integration with applications.
Always consider the nature of the data you intend to store, its potential range, and the precision required when choosing a data type.