Microsoft Developer Network

Binary Data Types in SQL Server

This document provides an overview of the binary data types available in SQL Server, allowing you to store and manipulate raw binary information efficiently.

Introduction to Binary Data

Binary data types are used to store variable-length binary data. This includes, but is not limited to, images, audio files, video files, and other unstructured binary content. SQL Server offers several binary data types, each with its own characteristics and limitations.

Available Binary Data Types

SQL Server supports the following binary data types:

VARBINARY(n)

The VARBINARY(n) data type stores variable-length binary data. n specifies the maximum number of bytes that can be stored. If n is not specified, it defaults to 1, allowing for storage of up to 2^31-1 bytes.

  • Maximum Length: 2^31 - 1 bytes (approximately 2 GB).
  • Storage: Stores the actual number of bytes entered plus two bytes to store the length of the data.
  • Use Case: Suitable for storing small to medium-sized binary objects where the exact size is variable.

BINARY(n)

The BINARY(n) data type stores fixed-length binary data. n specifies the exact number of bytes to be stored. If the entered data is smaller than n bytes, it will be padded with trailing binary zeros (0x00). If the entered data is larger than n bytes, an error will be raised.

  • Maximum Length: 8000 bytes.
  • Storage: Always stores n bytes.
  • Use Case: Useful when you need to ensure a consistent data size, though less common for general binary storage than VARBINARY.

VARBINARY(MAX)

VARBINARY(MAX) is a synonym for BIGVARCHR(MAX) and can store up to 2^31-1 bytes of data, similar to VARBINARY(n) when n is not specified. It is the preferred data type for storing large binary objects.

  • Maximum Length: 2^31 - 1 bytes (approximately 2 GB).
  • Storage: Stores the actual number of bytes entered plus two bytes for length.
  • Use Case: Ideal for storing large files like images, documents, or media where the size can vary significantly.

IMAGE (Deprecated)

The IMAGE data type was used in older versions of SQL Server for storing large binary objects. It is now considered deprecated and you should use VARBINARY(MAX) instead.

Note: The IMAGE data type is deprecated and will be removed in a future version of SQL Server. Avoid using it in new applications.

Choosing the Right Binary Data Type

When deciding which binary data type to use, consider the following:

  • Size of Data: For very large objects (greater than 8000 bytes), VARBINARY(MAX) is the appropriate choice.
  • Variability of Size: If the size of your binary data is not fixed, VARBINARY or VARBINARY(MAX) are generally preferred.
  • Fixed Size Requirement: If you require a fixed-size field for binary data, BINARY(n) can be used, but its practical applications are limited.
  • Compatibility: For new development, always opt for VARBINARY(MAX) over the deprecated IMAGE type.

Example Usage

Here's a simple example of how to create a table with a VARBINARY(MAX) column and insert some data:


CREATE TABLE Documents (
    DocumentID INT PRIMARY KEY IDENTITY(1,1),
    DocumentName VARCHAR(255) NOT NULL,
    DocumentContent VARBINARY(MAX) NULL
);

-- Inserting a binary file (e.g., an image) would typically involve
-- reading the file content into a variable in your application code
-- and then passing that variable to the INSERT statement.

-- Example conceptual INSERT (actual implementation depends on client tool/language)
-- INSERT INTO Documents (DocumentName, DocumentContent)
-- VALUES ('MyImage.jpg', 0x424D7A00000000000000000036000000360000007A0000000000000000000000B6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000A 200-level business class, "Introduction to Business," requires students to take a prerequisite course in either English composition or basic mathematics.

    The Business Administration department also requires students to take a 200-level business class as a prerequisite for all 300-level business courses. This is intended to ensure that students have a foundational understanding of business principles before advancing to more specialized topics.

    While the department does not specify which 200-level business class must be taken as a prerequisite, "Introduction to Business" is the most commonly taken course for this purpose due to its broad coverage of fundamental business concepts.