SQL UNICODE函数
结构化查询语言中的UNICODE函数显示字符串的第一个字符的Unicode(整数)值。我们也可以将UNICODE函数与SQL表的字符串字段一起使用。
UNICODE字符串函数的语法
语法1: 此语法使用UNICODE函数与SQL表的列名:
SELECT UNICODE (Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要使用UNICODE字符串函数的列名。
语法2: 此语法使用带有字符集(字符串)的UNICODE函数:
SELECT UNICODE (String);
语法2: 此语法使用带有个别字符的UNICODE函数:
SELECT UNICODE (character);
UNICODE字符串函数示例
示例1: 以下的SELECT查询显示了给定字符串的UNICODE值:
SELECT UNICODE (JAVATPOINT) AS ASCII_J;
输出:
ASCII_J
---
74
示例 2: 下面的SELECT查询返回@符号的UNICODE值:
SELECT UNICODE (@#$12453@#) AS ASCII_@;
输出:
ASCII_@
---
64
示例3: 下面的SELECT查询显示了给定字符串的第一个字符的UNICODE值:
SELECT UNICODE (New Delhi IS the Capital OF INDIA) AS ASCII_N;
输出:
ASCII_N
---
78
示例 3: 下面的SELECT查询显示了’R’字符的UNICODE值:
SELECT UNICODE (R) AS ASCII_R;
输出:
ASCII_R
---
82
示例 4:此示例使用SQL表中的UNICODE函数
在此示例中,我们将创建一个新的SQL表,我们需要对其执行UNICODE函数以找到第一个字符的UNICODE值。
在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列上使用了UNICODE函数:
SELECT Last_Name, UNICODE (Last_Name) AS UNICODE _LastName FROM Student_Grade;
此SQL语句显示表中姓氏的第一个字符的Unicode值。
输出:
Last_Name | UNICODE _LastName |
---|---|
SHARMA | 83 |
SHARMA | 83 |
GUPTA | 71 |
SINGHANIA | 83 |
ROY | 82 |
GUPTA | 71 |
GUPTA | 71 |
查询2: 下面的SELECT查询在上述Student_Grade表中使用First_City、Second_City和New_City列的UNICODE函数,只筛选出Roll_No大于2的学生:
SELECT Roll_No, UNICODE (First_City), UNICODE (Second_City), UNICODE (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 |
查询3: 以下SELECT查询使用UNICODE函数与上述Student_Grade表的First_Name列:
SELECT First_Name, UNICODE (First_Name) AS UNICODE _FirstName FROM Student_Grade;
这个SQL语句显示了表中第一个名字的第一个字符的Unicode值。
输出:
First_Name | UNICODE _FirstName |
---|---|
Aman | 65 |
Vishal | 86 |
Raj | 82 |
Yash | 90 |
Vinay | 86 |
Manoj | 77 |
Ram | 82 |