In MySQL, the OCT()
function is used for converting from decimal to octal.
More precisely, it returns a string representation of the octal value of its argument.
Syntax
The basic syntax goes like this:
OCT(N)
Where n
is the value to be converted. This argument is a longlong (BIGINT) number.
Example 1 – Basic Usage
Here’s an example of how it works:
SELECT OCT(8);
Result:
+--------+ | OCT(8) | +--------+ | 10 | +--------+
The result is 10
because that is the octal equivalent of 8 from the decimal system.
Example 2 – Various Values
Here’s another example with various values:
SELECT OCT(10), OCT(20), OCT(30), OCT(100), OCT(1000);
Result:
+---------+---------+---------+----------+-----------+ | OCT(10) | OCT(20) | OCT(30) | OCT(100) | OCT(1000) | +---------+---------+---------+----------+-----------+ | 12 | 24 | 36 | 144 | 1750 | +---------+---------+---------+----------+-----------+
Example 3 – Expressions
You can also use expressions like the ones below:
SELECT OCT(100 + 2), OCT(100 * 2), OCT(100 / 2), OCT(100 - 2);
Result:
+--------------+--------------+--------------+--------------+ | OCT(100 + 2) | OCT(100 * 2) | OCT(100 / 2) | OCT(100 - 2) | +--------------+--------------+--------------+--------------+ | 146 | 310 | 62 | 142 | +--------------+--------------+--------------+--------------+
What is Octal?
Octal is a system of numerical notation that has 8 as a base. This is in contrast to decimal, which has 10 as a base.
In decimal, we count up to 9, then start again by adding a zero after the first digit (e.g. after 9 comes 10, which is 1 with a zero added to it).
In octal (base 8) however, we only count to 7 before starting again and adding a zero. So 10 in octal is the equivalent of 8 in decimal.
Here’s a table to demonstrate:
Decimal (Base 10) | Octal (Base 8) |
---|---|
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 10 |
9 | 11 |
10 | 12 |
11 | 13 |
12 | 14 |
13 | 15 |
14 | 16 |
15 | 17 |
16 | 20 |
17 | 21 |
18 | 22 |
19 | 23 |
20 | 24 |