OCT() function in MySQL
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_id | Player_name | Playing_team | Run_Scored |
---|---|---|---|
1 | Virat Kohli | RCB | 60 |
2 | Rohit Sharma | MI | 45 |
3 | Dinesh Karthik | KKR | 26 |
4 | Shreyash Iyer | DC | 40 |
5 | David Warner | SRH | 65 |
6 | Steve Smith | RR | 52 |
7 | Andre Russell | KKR | 70 |
8 | Jasprit Bumrah | MI | 10 |
9 | Risabh Panth | DC | 34 |
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_id | Player_name | Playing_team | RunInOctal |
---|---|---|---|
1 | Virat Kohli | RCB | 74 |
2 | Rohit Sharma | MI | 55 |
3 | Dinesh Karthik | KKR | 32 |
4 | Shreyash Iyer | DC | 50 |
5 | David Warner | SRH | 101 |
6 | Steve Smith | RR | 64 |
7 | Andre Russell | KKR | 106 |
8 | Jasprit Bumrah | MI | 12 |
9 | Risabh Panth | DC | 42 |