# How the OCT() function works in Mariadb?

The `OCT()`

function is a built-in function in Mariadb that returns the octal representation of a decimal or string value.

The `OCT()`

function is a built-in function in Mariadb that returns the octal representation of a decimal or string value. Octal is a number system that uses eight symbols: 0, 1, 2, 3, 4, 5, 6, and 7. Each octal digit represents three binary bits. For example, the octal number 12 is equivalent to the binary number 001 010, which is equal to the decimal number 10.

## Syntax

The syntax of the `OCT()`

function is as follows:

```
OCT(N)
```

Where `N`

is a decimal or string value that can be converted to a decimal value. If `N`

is `NULL`

, the function returns `NULL`

. If `N`

is a negative number, the function returns a two’s complement representation of the octal value.

## Examples

### Example 1: Converting a positive decimal value to octal

The following example shows how to use the `OCT()`

function to convert a positive decimal value to octal:

```
SELECT OCT(15) AS Octal;
```

The output is:

```
+-------+
| Octal |
+-------+
| 17 |
+-------+
```

The function returns 17, which is the octal representation of 15.

### Example 2: Converting a negative decimal value to octal

The following example shows how to use the `OCT()`

function to convert a negative decimal value to octal:

```
SELECT OCT(-15) AS Octal;
```

The output is:

```
+------------------------+
| Octal |
+------------------------+
| 1777777777777777777761 |
+------------------------+
```

The function returns 1777777777777777777761, which is the two’s complement representation of the octal value of -15. The two’s complement is a way of representing negative numbers in binary systems. To obtain the two’s complement of a number, you invert all the bits and add one. For example, the binary representation of -15 is 1111 0001, which is obtained by inverting the bits of 15 (0000 1111) and adding one. The octal representation of 1111 0001 is 361, which is the same as the last three digits of the output.

### Example 3: Converting a string value to octal

The following example shows how to use the `OCT()`

function to convert a string value to octal:

```
SELECT OCT('123') AS Octal;
```

The output is:

```
+-------+
| Octal |
+-------+
| 173 |
+-------+
```

The function converts the string ‘123’ to a decimal value and then returns the octal representation of it. The decimal value of ‘123’ is 123, and the octal representation of 123 is 173.

## Related Functions

There are some other functions in Mariadb that are related to the `OCT()`

function. They are:

`BIN()`

: This function returns the binary representation of a decimal or string value. Binary is a number system that uses two symbols: 0 and 1. Each binary digit represents one bit. For example, the binary number 1010 is equivalent to the decimal number 10.`HEX()`

: This function returns the hexadecimal representation of a decimal or string value. Hexadecimal is a number system that uses 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Each hexadecimal digit represents four binary bits. For example, the hexadecimal number A is equivalent to the binary number 1010, which is equal to the decimal number 10.`CONV()`

: This function converts a number from one base to another. The base is the number of symbols used in a number system. For example, the base of octal is 8, the base of binary is 2, and the base of hexadecimal is 16.

## Conclusion

The `OCT()`

function is a useful function in Mariadb that allows you to convert decimal or string values to octal values. Octal is a number system that is often used in computer science and engineering. You can also use other functions like `BIN()`

, `HEX()`

, and `CONV()`

to convert numbers between different bases. I hope this article helped you understand how the `OCT()`

function works in Mariadb.