15.00 - COUNTSET - 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)

COUNTSET

Purpose  

Returns the count of the binary bits within the target_arg expression that are either set to 1 or set to 0 depending on the target_value_arg value.

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_value_arg

an optional integer value. Only a value of 0 or 1 is allowed.

If target_value_arg is not specified, the default is 1.

ANSI Compliance

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

Description

COUNTSET takes the target_arg input expression and counts the number of bits within the expression that are either set to 1 or set to 0, depending on the value of target_value_arg.

The target_value_arg parameter only accepts a value of 0 or 1. If a value for target_value_arg is not specified, the default value of 1 is used, and COUNTSET counts the bit values that are set to 1.

If any of the input arguments is NULL, the function returns NULL.

Argument Types and Rules

COUNTSET is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg[, target_value_arg]) input combinations:

 

target_arg type

target_value_arg type (optional)

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

The result data type is INTEGER.

The result format is the default format for INTEGER.

The default title for COUNTSET is: COUNTSET(target_arg[, target_value_arg]).

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

Example  

The following query takes the input argument 23, which has a data type of BYTEINT and a binary representation of 00010111. Since target_value_arg is not specified, the default value of 1 is used. Therefore, the function counts the number of bit values that are set to 1. The query result is an INTEGER value of 4.

   SELECT COUNTSET(23);