TO_OCT

Convert an integer to its octal (base-8) string representation.

Category: numericReturns: STRINGDialect: PostgreSql

Syntax

TO_OCT(n)

Description

## Overview Returns the octal (base-8) string representation of the given integer. TO_OCT is the historical companion to TO_BIN and TO_HEX and is most commonly used for Unix-style permission masks and compact representation of groups of three bits. ## Behavior - Accepts INTEGER or BIGINT input. - Returns a STRING without leading zeros and without a base prefix. - Returns NULL if the argument is NULL. - TO_OCT(0) returns the string '0'. - Negative values are represented in two's complement at the full width of the input type. ## Numeric precision - TO_OCT is exact: every bit of the input is preserved in the output, grouped into digits of three bits. - The length of the output is variable (ceil((log2(|n|) + 1) / 3) for positive n, full width for negative n). ## Compatibility - Matches the PostgreSQL TO_OCT function. - Complementary to TO_BIN and to engine-specific TO_HEX or HEX functions.

Parameters

NameTypeDescription
nSpecifies the integer value to convert. Accepts INTEGER or BIGINT types. Negative values are represented using two's complement notation with the full width of the input type.

Examples

-- Octal of 8
SELECT TO_OCT(8);  -- '10'
-- Octal of 0
SELECT TO_OCT(0);  -- '0'
-- Octal of 255
SELECT TO_OCT(255);  -- '377'
-- Octal of 64
SELECT TO_OCT(64);  -- '100'
-- Octal of 511
SELECT TO_OCT(511);  -- '777'
-- NULL propagation
SELECT TO_OCT(NULL);  -- NULL

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →