ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from the numeric values of its constituent bytes using this formula :
(1st byte code)+ (2nd byte code * 256)+ (3rd byte code * 256^2) ......
Syntax :
ORD( str )
Parameter : This function accepts one parameter as mentioned above and described below :
- str : Given string whose left most character code is to be find.
Returns : It returns the code of the leftmost character in a string.
Example-1 : Applying ORD() Function to a single character.
SELECT ORD('S') as Find_Code;
Output :
Example-2 : Applying ORD() Function to a String.
SELECT ORD('geeksforgeeks') as Find_Code;
Output :
Example-3 : Applying ORD() Function to a number.
SELECT ORD(100) as Find_Code;
Output :
Example-4 :
The ORD function can also be used to find the code of the leftmost character of a column data. To demonstrate 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,
PRIMARY KEY(Player_id )
);
Now inserting some data to the Player table :
INSERT INTO
Player(Player_name ,Playing_team)
VALUES
('Virat Kohli' , 'RCB' ) ,
('Rohit Sharma' , 'MI' ) ,
('Dinesh Karthik', 'KKR' ) ,
('Shreyash Iyer' , 'DC' ) ,
('David Warner' , 'SRH' ) ,
('Steve Smith' , 'RR' ) ,
('Andre Russell' , 'KKR' ) ,
('Jasprit Bumrah' , 'MI' ) ,
('Risabh Panth', 'DC' ) ;
So, the Player Table is :
mysql> SELECT * FROM Player;
+-----------+----------------+--------------+
| Player_id | Player_name | Playing_team |
+-----------+----------------+--------------+
| 1 | Virat Kohli | RCB |
| 2 | Rohit Sharma | MI |
| 3 | Dinesh Karthik | KKR |
| 4 | Shreyash Iyer | DC |
| 5 | David Warner | SRH |
| 6 | Steve Smith | RR |
| 7 | Andre Russell | KKR |
| 8 | Jasprit Bumrah | MI |
| 9 | Risabh Panth | DC |
+-----------+----------------+--------------+
Now, we will apply ORD function to find the code of the leftmost character of column Player_name and Playing_team.
Select
* ,
ORD(Player_name) ,
ORD(Playing_team)
FROM Player;
Output :
+-----------+----------------+--------------+------------------+-------------------+
| Player_id | Player_name | Playing_team | ORD(Player_name) | ORD(Playing_team) |
+-----------+----------------+--------------+------------------+-------------------+
| 1 | Virat Kohli | RCB | 86 | 82 |
| 2 | Rohit Sharma | MI | 82 | 77 |
| 3 | Dinesh Karthik | KKR | 68 | 75 |
| 4 | Shreyash Iyer | DC | 83 | 68 |
| 5 | David Warner | SRH | 68 | 83 |
| 6 | Steve Smith | RR | 83 | 82 |
| 7 | Andre Russell | KKR | 65 | 75 |
| 8 | Jasprit Bumrah | MI | 74 | 77 |
| 9 | Risabh Panth | DC | 82 | 68 |
+-----------+----------------+--------------+------------------+-------------------+
Similar Reads
RPAD() Function in MySQL
RPAD() function in MySQL is used to pad or add a string to the right side of the original string. Syntax : RPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below : str : The actual string which is to be padded. If the length of the original st
1 min read
RAND() Function in MySQL
The RAND() function in MySQL is used to a return random floating-point value V in the range 0 <= V < 1.0. If we want to obtain a random integer R in the range i <= R < j, we have to use the expression : FLOOR(i + RAND() * (j â i)). Syntax : RAND(N) Parameter : This method accepts only on
3 min read
POWER() Function in MySQL
POWER() function in MySQL is used to find the value of a number raised to the power of another number. It Returns the value of X raised to the power of Y. Syntax : POWER(X, Y) Parameter : This method accepts two parameter which are described below : X : It specifies the base number. Y : It specifies
3 min read
ROUND() Function in MySQL
The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round-off, it rounds off the number to the nearest integer. Syntax ROUND(X, D) Parameter Explanation This method accepts two parameters in the syn
2 min read
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 numb
2 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
BIT_OR() Function in MySQL
BIT_OR() function in MySQL is used to return the bitwise OR of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise or operation on those binary values. Syntax : BIT_OR(expr) Parameter : This method accepts only one parameter. expr - Input
4 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
TRIM() Function in MySQL
TRIM() function in MySQL is used to clean up data. It is also used to remove the unwanted leading and trailing characters in a string. Syntax : TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) Parameter : This method accepts three-parameter as mentioned above and described below : BOTH | LEADIN
2 min read