SQL ASCII函数

SQL ASCII函数

结构化查询语言中的ASCII函数显示字符串的第一个字符的ASCII值。我们也可以将ASCII函数与SQL表的字符串字段一起使用。

ASCII字符串函数的语法

语法1: 这个语法将ASCII函数与SQL表的列名一起使用:

SELECT ASCII (Column_Name) AS Alias_Name FROM Table_Name;

在语法中,我们必须指定要使用ASCII字符串函数的列名。

语法2: 此语法使用ASCII函数与字符集(字符串):

SELECT ASCII (String);

语法2: 这个语法使用ASCII函数和单个字符:

SELECT ASCII (character);

ASCII字符串函数示例

示例 1: 下面的SELECT查询显示了给定字符串的ASCII值:

SELECT ASCII (JAVATPOINT) AS ASCII_J;

输出结果:

ASCII_J

74

示例3: 以下SELECT查询显示给定字符串的第一个字符的ASCII值:

SELECT ASCII (New Delhi IS the Capital OF INDIA) AS ASCII_N;

输出:

ASCII_N

78

示例 3: 下面的SELECT查询显示了’R’字符的ASCII值:

SELECT ASCII (R) AS ASCII_R;

输出:

ASCII_R

82

示例4:此示例使用ASCII函数与SQL表格

在这个示例中,我们将创建一个新的SQL表格,在该表格上我们必须执行ASCII函数来查找第一个字符的ASCII值。

在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)
);

以下是将学生成绩和成绩记录插入到 Student_Grade 表中的INSERT查询:

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列的ASCII函数:

SELECT Last_Name, ASCII (Last_Name) AS ASCII _LastName FROM Student_Grade;

SQL 语句显示表中姓氏的第一个字符的 ASCII 值。

输出结果:

Last_Name ASCII _LastName
SHARMA 83
SHARMA 83
GUPTA 71
SINGHANIA 83
ROY 82
GUPTA 71
GUPTA 71

查询2: 下面的SELECT查询在上述Student_Grade表中,使用了ASCII函数来处理Roll_No大于2的学生的First_City、Second_City和New_City列:

SELECT Roll_No, ASCII (First_City), ASCII (Second_City), ASCII (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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程