Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

OCT() function in MySQL

  • Last Updated : 01 Oct, 2020

OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number.

Syntax :

OCT(number)

Parameter : This method accepts only one parameter.

  • number : The decimal number which we want to convert.

Returns : It returns octal value of a decimal number.

Example-1 :
Octal representation of the decimal number 0 using OCT Function.

SELECT OCT(0) AS Oct_number ;

Output :

Oct_number
0

Example-2 :
Octal representation of the decimal number 2020 using OCT Function.

SELECT OCT( 2020 ) AS Oct_number ;

Output :

Oct_number
3744

Example-3 :
Using OCT Function to find octal representation of all decimal number present in a column. To demonstrate, let us create a table named Player.

CREATE TABLE Player(

Player_id INT AUTO_INCREMENT,  
Player_name VARCHAR(100) NOT NULL,
Playing_team VARCHAR(20) NOT NULL,
Run_Scored INT NOT NULL,
PRIMARY KEY(Player_id )

);

Now, insert some data to the Player table –

INSERT INTO  
Player(Player_name, Playing_team, Run_Scored)
VALUES
('Virat Kohli', 'RCB', 60 ),
('Rohit Sharma', 'MI', 45),
('Dinesh Karthik', 'KKR', 26 ),
('Shreyash Iyer', 'DC', 40 ),
('David Warner', 'SRH', 65),
('Steve Smith', 'RR', 52 ),
('Andre Russell', 'KKR', 70),
('Jasprit Bumrah', 'MI', 10),
('Risabh Panth', 'DC', 34 ) ;

So, the Player Table is –

SELECT * FROM Player;
Player_idPlayer_namePlaying_teamRun_Scored
1Virat KohliRCB60
2Rohit SharmaMI45
3Dinesh KarthikKKR26
4Shreyash IyerDC40
5David WarnerSRH65
6Steve SmithRR52
7Andre RussellKKR70
8Jasprit BumrahMI10
9Risabh PanthDC34

Now, we will find run scored by each player in octal number using OCT Function.

SELECT  
Player_id, Player_name,
Playing_team, OCT(Run_Scored) AS RunInOctal
FROM Player ;

Output :

Player_idPlayer_namePlaying_teamRunInOctal
1Virat KohliRCB74
2Rohit SharmaMI55
3Dinesh KarthikKKR32
4Shreyash IyerDC50
5David WarnerSRH101
6Steve SmithRR64
7Andre RussellKKR106
8Jasprit BumrahMI12
9Risabh PanthDC42

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!