15.00 - GETBIT - Teradata Database

Teradata Database SQL Functions, Operators, Expressions, and Predicates

Product
Teradata Database
Release Number
15.00
Content Type
Programming Reference
Publication ID
B035-1145-015K
Language
English (United States)

GETBIT

Purpose  

Returns the value of the bit specified by target_bit_arg from the target_arg byte expression.

Syntax  

where:

 

Syntax element…

Specifies…

TD_SYSFNLIB

the name of the database where the function is located.

target_arg

a numeric or variable byte expression.

target_bit_arg

an integer expression.

ANSI Compliance

This is a Teradata extension to the ANSI SQL:2011 standard.

Description

GETBIT gets the bit specified by target_bit_arg from the target_arg byte expression and returns either 0 or 1 to indicate the value of that bit.

The range of input values for target_bit_arg can vary from 0 (bit 0 is the least significant bit) to the (sizeof(target_arg) - 1).

If target_bit_arg is negative or out-of-range (meaning that it exceeds the size of target_arg), an error is returned.

If either input argument is NULL, the function returns NULL.

Argument Types and Rules

GETBIT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, target_bit_arg) input combinations:

 

target_arg type

target_bit_arg type

BYTEINT

INTEGER

SMALLINT

INTEGER

INTEGER

INTEGER

BIGINT

INTEGER

VARBYTE(n)

INTEGER

The maximum supported size (n) for VARBYTE is 8192 bytes.

All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs.

Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming.

If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted.

For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming.

Result Type and Attributes

GETBIT returns a BYTEINT value of either 0 or 1, reflecting the value of the bit residing at the target_bit_arg position of the target_arg byte expression.

The result format is the default format for BYTEINT.

The default title for GETBIT is: GETBIT(target_arg, target_bit_arg).

For information on default data type formats, see SQL Data Types and Literals.

Example  

The following query gets the value of the third bit of the input argument 23, which has a data type of BYTEINT and a binary representation of 00010111. The query result is a BYTEINT value of 1 or binary 00000001.

   SELECT GETBIT(23,2);