A Quick Introduction to the GET_BIT() Function in SQL Server

In SQL Server, we can use the GET_BIT() function to return the bit from the specified location in a value. We pass the value as either an integer or binary expression.

The function accepts two arguments; the expression, and the offset. The function then returns the bit from the given offset in the expression.

Example

Here’s an example to demonstrate the function:

SELECT GET_BIT(12345, 1);

Result:

0

Here, I get the first bit from the integer value 12345.

The binary representation of 12345 is 0011 0000 0011 1001, and so the value returned is 0.

Let’s get the third bit:

SELECT GET_BIT(12345, 3);

Result:

1

This time we can see that the third bit is set to 1.

Passing a Binary Value

Here’s the binary equivalent of the above integer value:

SELECT GET_BIT(0x00003039, 3);

Result:

1

Same result.