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
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
POW() Function in MySQL
POW() function : This function in MySQL is used to return a results after raising a specified exponent number to a specified base number. For example if the base is 5 and exponent is 2, this will return a result of 25. Syntax : SELECT POW(x, y); Parameter : This method accepts two parameters as give
1 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
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
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
QUOTE () function in MySQL
QUOTE() : This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash. I
2 min read