Home >Database >Mysql Tutorial >Implementation method of converting decimal to octal in MySQL

Implementation method of converting decimal to octal in MySQL

藏色散人
藏色散人Original
2019-04-04 09:29:493019browse

In MySQL, the OCT() function is used to convert decimal to octal. More precisely, it returns the string representation of the argument's octal value. (Related recommendation: "MySQL Tutorial")

The basic syntax is this:

OCT(N)

where n is the value to be converted. This parameter is a longlong (BIGINT) number

Example 1 - Basic usage

SELECT OCT(8);

Result:

+--------+
| OCT(8) |
+--------+
| 10     |
+--------+

The result is 10, because This is the octal equivalent of 8 in decimal.

Example 2 - Different Values

Here is another example of different 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 - Expression

You can also use the following expression:

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 number symbol system with base 8. This is in contrast to decimal, which is base 10.

In decimal, we count up to 9 and then add a 0 after the first number (for example, 9 is followed by 10, which is 1 plus a 0).

However, in octal (base 8) we only count to 7 and then start over and add a 0. So 10 in octal and 8 in decimal are equivalent.

The table is as follows:

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

This article is about the implementation method of converting decimal to octal in MySQL. I hope it will be helpful to friends in need!

The above is the detailed content of Implementation method of converting decimal to octal in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn