DEC2BIN
Updated 2023-11-08 15:59:39.657000
Syntax
SELECT [westclintech].[wct].[DEC2BIN] (
<@Number, float,>
,<@Places, float,>)
Description
Use the scalar function DEC2BIN to convert a decimal number to binary.
Arguments
@Places
The number of characters to use. If @Places is NULL, DEC2BIN uses the minimum number of characters necessary. @Places is useful for padding the return value with leading 0s (zeros). @Places is an expression of type float or of a type that can be implicitly converted to float.
@Number
is the decimal integer you want to convert. If @Number is negative, valid @Places values are ignored and DEC2BIN returns a 10-character (10-bit) binary number in which the most significant bit is the sign bit. The remaining 9 bits are magnitude bits. Negative numbers are represented using two's-complement notation. @Number is an expression of type float or of a type that can be implicitly converted to float.
Return Type
nvarchar(4000)
Remarks
If @Number is less than -512 or @Number is greater than 511, DEC2BIN returns an error.
If @Number is negative, DEC2BIN ignores places and returns a 10-character binary number.
@Places is truncated to zero decimal places.
Examples
select wct.DEC2BIN(-512, NULL);
This produces the following result.
{"columns":[{"field":"column 1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"column 1":"1000000000"}]}
select wct.DEC2BIN(511, NULL);
This produces the following result.
{"columns":[{"field":"column 1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"column 1":""},{"column 1":"111111111"}]}
select wct.DEC2BIN(11, 10);
This produces the following result.
{"columns":[{"field":"column 1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"column 1":"000001011"}]}