MSDN Documentation

SQL Server Data Types

Understanding data types is fundamental to efficient and correct database design and programming in SQL Server. Data types define the kind of data that can be stored in a column, variable, or parameter, and they dictate the operations that can be performed on that data.

Categories of Data Types

SQL Server data types can be broadly categorized into the following groups:

  • Exact Numerics: Store precise numeric values.
  • Approximate Numerics: Store approximate numeric values, suitable for floating-point numbers.
  • Date and Time: Store date and time values.
  • Character Strings: Store text data, both fixed-length and variable-length.
  • Unicode Character Strings: Store text data with full Unicode support.
  • Binary Strings: Store binary data, such as images or other large objects.
  • Other Data Types: Including spatial data, XML, JSON, and unique identifiers.

Common Data Types and Their Usage

Here's a look at some of the most commonly used data types:

Data Type Description Example Usage
INT Standard integer. Stores whole numbers from -2,147,483,648 to 2,147,483,647. User IDs, counts, quantities.
BIGINT Large integer. Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Large transaction IDs, row counts exceeding INT limits.
DECIMAL(p,s) / NUMERIC(p,s) Exact numeric with fixed precision and scale. p is total digits, s is digits after decimal. Monetary values, financial calculations. e.g., DECIMAL(10, 2) for currency.
FLOAT / REAL Approximate floating-point numbers. Suitable for scientific calculations where exact precision is not critical. Scientific measurements, sensor readings.
VARCHAR(n) Variable-length non-Unicode character string. Up to 8000 characters. Names, addresses, product descriptions.
NVARCHAR(n) Variable-length Unicode character string. Up to 4000 characters (each character takes 2 bytes). Text that needs to support international characters (e.g., names, product titles in multiple languages).
DATE Stores a date (year, month, day). Birthdays, order dates, appointment dates.
DATETIME Stores a date and time. Precision up to .003 seconds. Log entry timestamps, event times.
DATETIME2 More precise date and time storage with a configurable fractional second precision. High-precision timestamps for critical operations.
BIT Boolean value (0, 1, or NULL). Flags, true/false indicators.
UNIQUEIDENTIFIER Globally unique identifier (GUID). Primary keys, unique record identifiers.
VARBINARY(n) Variable-length binary data. Up to 8000 bytes. Small images, serialized objects.
XML Stores XML data. Configuration settings, structured document storage.
JSON Stores JSON data. Application settings, semi-structured data.

Choosing the Right Data Type

Selecting the appropriate data type is crucial for several reasons:

  • Storage Efficiency: Using the smallest data type that can accommodate your data minimizes storage requirements. For example, use SMALLINT instead of INT if your values will never exceed 32,767.
  • Data Integrity: Data types enforce constraints on the kind of data that can be entered, preventing invalid or inconsistent data.
  • Performance: Smaller data types often lead to faster query execution as less data needs to be processed.
  • Accuracy: Using exact numeric types (like DECIMAL) for financial data prevents rounding errors that can occur with approximate types (like FLOAT).

Example: Creating a Table with Various Data Types

Here's a T-SQL snippet demonstrating the creation of a sample table using different data types:


CREATE TABLE Products (
    ProductID INT PRIMARY KEY IDENTITY(1,1),
    ProductName VARCHAR(100) NOT NULL,
    Category NVARCHAR(50),
    Price DECIMAL(10, 2) CHECK (Price >= 0),
    StockQuantity INT DEFAULT 0,
    IsActive BIT DEFAULT 1,
    DateAdded DATETIME2 DEFAULT GETDATE()
);
                

Further Reading

For a comprehensive list and detailed explanation of all SQL Server data types, please refer to the official Microsoft documentation:

SQL Server Data Types (Microsoft Docs)