Which database type is a collection of binary data stored in a single field of a database?

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

binary and varbinary (Transact-SQL)

  • Article
  • 09/22/2022
  • 2 minutes to read

In this article

Applies to:

Which database type is a collection of binary data stored in a single field of a database?
SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW)

Binary data types of either fixed length or variable length.

Arguments

binary [ ( n ) ]

Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. The storage size is n bytes.

varbinary [ ( n | max ) ]

Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length. The ANSI SQL synonym for varbinary is binary varying.

Remarks

The default length is 1 when n isn't specified in a data definition or variable declaration statement. When n isn't specified with the CAST function, the default length is 30.

Data typeUse when ...
binary the sizes of the column data entries are consistent.
varbinary the sizes of the column data entries vary considerably.
varbinary(max) the column data entries exceed 8,000 bytes.

Convert binary and varbinary data

When converting data from a string data type to a binary or varbinary data type of unequal length, SQL Server pads or truncates the data on the right. These string data types are:

  • char
  • varchar
  • nchar
  • nvarchar
  • binary
  • varbinary
  • text
  • ntext
  • image

When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.

Converting data to the binary and varbinary data types is useful if binary data is the easiest way to move around data. At some point, you might convert a value type to a binary value of large enough size and then convert it back. This conversion always results in the same value if both conversions are taking place on the same version of SQL Server. The binary representation of a value might change from version to version of SQL Server.

You can convert int, smallint, and tinyint to binary or varbinary. If you convert the binary value back to an integer value, this value will be different from the original integer value if truncation has occurred. For example, the following SELECT statement shows that the integer value 123456 is stored as a binary 0x0001e240:

SELECT CAST( 123456 AS BINARY(4) );

However, the following SELECT statement shows that if the binary target is too small to hold the entire value, the leading digits are silently truncated so that the same number is stored as 0xe240:

SELECT CAST( 123456 AS BINARY(2) );

The following batch shows that this silent truncation can affect arithmetic operations without raising an error:

DECLARE @BinaryVariable2 BINARY(2);
  
SET @BinaryVariable2 = 123456;
SET @BinaryVariable2 = @BinaryVariable2 + 1;
  
SELECT CAST( @BinaryVariable2 AS INT);
GO

The final result is 57921, not 123457.

Note

Conversions between any data type and the binary data types are not guaranteed to be the same between versions of SQL Server.

See also

  • CAST and CONVERT (Transact-SQL)
  • Data Type Conversion (Database Engine)
  • Data Types (Transact-SQL)

Which database data type is a collection of binary data stored in a single field of a database?

A binary large object (BLOB or blob) is a collection of binary data stored as a single entity.

Which part of a database is where a single piece of data is stored?

1) In a database table, a field is a data structure for a single piece of data. Fields are organized into records, which contain all the information within the table relevant to a specific entity.

What is relational database and non

A relational database is the database management system in which data is stored in distinct tables from where they can be accessed or reassembled in different ways under user-defined relational tables, whereas a Non-Relational Database is the database architecture that is not built around tables.

What are the two types of data stored by the database system?

Two types of database structure.
A flat file database stores data in a plain text file, with each line of text typically holding one record. ... .
A relational database contains multiple tables of data with rows and columns that relate to each other through special key fields..