Tuesday 30 July 2019

How the OCT() Function Works in MySQL

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)
11
22
33
44
55
66
77
810
911
1012
1113
1214
1315
1416
1517
1620
1721
1822
1923
2024

0 comments:

Post a Comment