แก้ไข

แชร์ผ่าน


GET_BIT (Transact SQL)

Applies to: SQL Server 2022 (16.x) Azure SQL Database Azure SQL Managed Instance SQL analytics endpoint in Microsoft Fabric Warehouse in Microsoft Fabric

GET_BIT takes two parameters and returns the bit in expression_value that is in the offset defined by bit_offset.

Transact-SQL syntax conventions

Syntax

GET_BIT ( expression_value, bit_offset )

Arguments

expression_value

Any integer or binary expression that isn't a large object (LOB).

bit_offset

Any integer.

Return types

bit

The bit_offset parameter in GET_BIT is used to identify the nth bit of the data to get or set. In integer types, the 0th bit is the least significant bit. In binary types, the 0th bit is the least significant bit in the rightmost byte.

GET_BIT will throw an error if bit_offset is negative or greater than the last bit in the data type.

Remarks

Distributed Query functionality for the bit manipulation functions within linked server or ad hoc queries (OPENQUERY) aren't supported.

Large object (LOB) data types in the Database Engine can store data that exceeds 8,000 bytes. These data types store data on a row-overflow data page. A LOB also encompasses data types that store data on dedicated LOB page structures, which use a text or an image pointer of in-row references to LOB data pages. For more information about data storage, see the Pages and extents architecture guide.

The bit manipulation functions operate on the tinyint, smallint, int, bigint, binary(n), and varbinary(n) data types. Large object (LOB) data types, such as varchar(max), nvarchar(max), varbinary(max), image, ntext, text, xml, and common language runtime (CLR) BLOB types, aren't supported.

Examples

In this example, the second and fourth bits are returned.

SELECT GET_BIT ( 0xabcdef, 2 ) as Get_2nd_Bit,
GET_BIT ( 0xabcdef, 4 ) as Get_4th_Bit;

The results are as follows:

Get_2nd_Bit Get_4th_Bit
1 0

Note

0xabcdef in binary is 1010 1011 1100 1101 1110 1111. The second and fourth bits are highlighted.