SQL ORD函数
结构化查询语言中的ORD函数显示指定字符串或单词最左字符的代码。SQL还允许对表的字符串字段执行ORD函数。
ORD字符串函数的语法
语法1: 此语法使用带有SQL表的列名的ORD函数:
SELECT ORD(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要使用ORD字符串函数的列名。
语法2: 此语法使用ORD函数与字符集(字符串):
SELECT ORD(String);
语法2: 这个语法使用ORD函数和每个字符:
SELECT ORD(character);
示例:ORD字符串函数
示例 1: 下面的SELECT查询显示了给定字符串的ORD值:
SELECT ORD (JAVATPOINT) AS ASCII_J;
输出:
ASCII_J
---
74
示例2: 下面的SELECT查询返回@符号的ORD值:
SELECT ORD (@#$12453@#) AS ASCII_@;
输出:
ASCII_@
---
64
例子3: 下面的SELECT查询显示给定字符串的第一个字符的ORD值:
SELECT ORD (New Delhi IS the Capital OF INDIA) AS ASCII_N;
输出:
ASCII_N
---
78
示例 3: 下面的SELECT查询显示了字符’R’的ORD值:
SELECT ORD (R) AS ASCII_R;
输出:
ASCII_R
---
82
示例4:本示例使用ORD函数和SQL表
在这个示例中,我们将创建一个新的SQL表,在这个表上我们要执行ORD函数来查找第一个字符的ORD值。
在SQL数据库中创建新表的语法如下:
CREATE TABLE table_name
(
First_Column_of_SQLtable Data Type (character_size of First Column),
Second_Column_of_SQLtable Data Type (character_size of the Second column ),
Third_Column_of_SQLtable Data Type (character_size of the Third column),
...
Last_Column_of_table Data Type (character_size of the Last column)
);
以下CREATE语句创建了 Student_Grade 表:
CREATE TABLE Student_Grade
(
Roll_No INT PRIMARY KEY,
First_Name VARCHAR (100),
Last_Name VARCHAR (100),
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Hindi_Marks INT,
Maths_Marks INT,
Grade Varchar (80)
);
以下INSERT查询插入了学生的成绩和分数记录到 Student_Grade 表中:
INSERT INTO Student_Grade (Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) VALUES (10, Aman, Sharma, Lucknow Chandigarh, Ghaziabad, 88, 95, A2);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES ( 02, Vishal, Sharma, Chandigarh, Ghaziabad, Delhi, 95, 82, A1 );
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (07, Raj, Gupta, Delhi, Ghaziabad, Lucknow, 91, 95, A1);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (04, Yash, Singhania, Ghaziabad, Delhi, Lucknow, 85, 82, A2);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (11, Vinay, Roy, Delhi, Kanpur, Ghaziabad, 95, 97, A1);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, Chandigarh, 95, 90, B1);
INSERT INTO Student_Grade
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)
VALUES (19, Ram, Gupta, Lucknow, Ghaziabad, Chandigarh, 89, 95, A2);
以下SELECT语句显示了上述插入的记录 Student_Grade 表:
SELECT * FROM Student_Grade;
Roll_No | First_Name | Last_Name | First_City | Second_City | New_City | Hindi_Marks | Maths_Marks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Aman | Sharma | Lucknow | Chandigarh | Ghaziabad | 88 | 95 | A2 |
02 | Vishal | Sharma | Chandigarh | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Raj | Gupta | Delhi | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Yash | Singhania | Ghaziabad | Delhi | Lucknow | 85 | 82 | A2 |
11 | Vinay | Roy | Delhi | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Manoj | Gupta | Ghaziabad | Meerut | Chandigarh | 95 | 90 | B1 |
19 | Ram | Gupta | Lucknow | Ghaziabad | Chandigarh | 89 | 95 | A2 |
查询1: 下面的SELECT查询使用了上述Student_Grade表的Last_Name列的ORD函数:
SELECT Last_Name, ORD(Last_Name) AS ORD_LastName FROM Student_Grade;
此SQL语句显示了表中姓氏最左边字符的代码。
输出:
Last_Name | ORD_LastName |
---|---|
SHARMA | 83 |
SHARMA | 83 |
GUPTA | 71 |
SINGHANIA | 83 |
ROY | 82 |
GUPTA | 71 |
GUPTA | 71 |
查询2: 以下SELECT查询使用ORD函数及上述学生成绩表中Roll_No大于2的学生的First_City、Second_City和New_City列:
SELECT Roll_No, ORD (First_City), ORD (Second_City), ORD (New_City) FROM Student_Grade WHERE Roll_No > 2;
输出:
Roll_No | ASCII(First_City) | ASCII(Second_City) | ASCII(New_City) |
---|---|---|---|
07 | 68 | 71 | 76 |
04 | 71 | 68 | 76 |
11 | 68 | 75 | 71 |
16 | 71 | 77 | 67 |
19 | 76 | 71 | 67 |