MySQL - OCT() Function



The MySQL OCT() function accepts a number of the type longlong (BIGINT) representing a binary value as a parameter, and converts it into an octal value and returns its string representation.

If a string is passed as input to this function, it returns 0 as the result. When the input value is NULL, it returns NULL.

Syntax

Following is the syntax of MySQL OCT() function −

OCT(N);

Parameters

This function takes an integer value as a parameter.

Return Value

This function returns the octal representation of the given value as a string.

Example

In the following example, we are using the OCT() function to convert the decimal number '225' into its octal representation −

SELECT OCT(225);

Following is the output of the above code −

OCT(225)
341

Example

If you pass a string value as an argument to this function, it returns 0 −

SELECT OCT('tutorialspoint');

The output obtained is as follows −

OCT('tutorialspoint')
0

Example

In here, we are passing an integer as a string value to this function −

SELECT OCT('554566666');

We get the output as follows −

OCT('554566666')
4103402012

Example

If you pass a NULL value to this function, it returns NULL −

SELECT OCT(NULL);

Following is the output of the above code −

OCT(NULL)
NULL

Example

You can also convert the entities of a column into an octal value using the OCT() function.

Let us create a table named "EMP" and insert records into it using CREATE and INSERT statements as shown below −

CREATE TABLE EMP(
   FIRST_NAME  CHAR(20) NOT NULL,
   LAST_NAME  CHAR(20),
   AGE INT,
   INCOME FLOAT
);

Now, let us insert records into it using the INSERT statement −

INSERT INTO EMP VALUES 
('Krishna', 'Sharma', 19, 2000),
('Raj', 'Kandukuri', 20, 7000),
('Ramya', 'Ramapriya', 25, 5000),
('Mac', 'Mohan', 26, 2000);

The EMP obtained is as follows −

FIRST_NAME LAST_NAME AGE INCOME
Krishna Sharma 19 2000
Raj Kandukuri 20 7000
Ramya Ramapriya 25 5000
Mac Mohan 26 2000

Following query retrieve the octal representations of the 'INCOME' values using the OCT() function −

SELECT FIRST_NAME, LAST_NAME, AGE, OCT(INCOME) 
FROM EMP;

After executing the above code, we get the following output −

FIRST_NAME LAST_NAME AGE OCT(INCOME)
Krishna Sharma 19 3720
Raj Kandukuri 20 15530
Ramya Ramapriya 25 11610
Mac Mohan 26 3720
mysql-oct-function.htm
Advertisements