SQL CHAR函数
CHAR字符串函数显示函数中传递的整数的ASCII值。此函数只接受一个参数。如果我们传递的整数值超过给定范围,则显示NULL值。
CHAR字符串函数的语法
在SQL中,我们可以将CHAR函数与表的列、字符串和字符一起使用。
语法1:
SELECT CHAR(Integer_Column_Name) AS Alias_Name FROM Table_Name;
在这个语法中,我们使用了SQL的现有表的CHAR函数。在这里,我们必须定义表的名称和整数列,我们想要执行CHAR函数。
语法2:
SELECT CHAR(Integer_Value) AS Alias_Name;
在这个语法中,我们使用了带有整数值的CHAR函数。
CHAR字符串函数的示例
示例1: 下面的SELECT查询显示了20的ASCII值:
SELECT CHAR(20)AS ASCII_of_20;
输出:
ASCII_of_20
---
^T
示例2: 以下的SELECT查询显示了字符”84″的ASCII值:
SELECT CHAR(98)AS ASCII_of_84;
输出:
ASCII_of_84
---
T
示例3: 下面的SELECT查询显示了260的ASCII值:
SELECT CHAR(260)AS ASCII_of_260;
输出:
ASCII_of_98
---
NULL
示例4:本示例使用SQL中的CHAR函数与表格进行操作。
在这第四个示例中,我们将通过新表来执行CHAR函数与表格的值一起操作。
以下代码块展示了在SQL中创建新表的语法:
CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),
Second_Column_of_table Data Type (character_size of the Second column ),
Third_Column_of_table Data Type (character_size of the Third column),
........,
Last_Column_of_table Data Type (character_size of the Last column)
);
下面的CREATE语句创建了 Fresher_Marks 表格:
CREATE TABLE Fresher_Marks
(
Fresher_ID INT PRIMARY KEY,
First_Name VARCHAR (100),
Middle_Name Varchar(120),
Last_Name VARCHAR (200),
City Varchar(120),
Aptitude_Marks INT,
Reasoning_Marks INT,
Technical_Marks INT,
Percentage INT
);
以下INSERT查询将新人的成绩和详细信息插入 Fresher_Marks 表中:
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (501, Vinay, Roy, Gupta, Lucknow, 85, 92, 78, 85 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (92, Monu, Roy, Singhania, Chandigarh, 54, 68, 98, 88 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (104, Ravi, Roy, Kumar, Lucknow, 71, 82, 69, 71 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (7, Shyam, Roy, Sharma, Delhi, 85, 90, 68, 78 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (85, Abhay, Kumar, Gupta, Chandigarh, 45, 68, 82, 78);
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (509, Riya, Roy, Sharma, Delhi, 68, 90, 69, 91 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (65, Vishal, Kumar, Sharma, Mumbai, 75, 65, 88, 75 );
下面的SELECT语句显示了上述 Fresher_Marks 表的插入记录:
SELECT * FROM Fresher_Marks;
Fresher_ID | First_Name | Middle_Name | Last_Name | City | Aptitude_Marks | Reasoning_Marks | Technical_Marks | Percentage |
---|---|---|---|---|---|---|---|---|
501 | Vinay | Roy | Gupta | Lucknow | 85 | 92 | 78 | 85 |
92 | Monu | Roy | Singhania | Chandigarh | 54 | 68 | 98 | 88 |
104 Ravi | Roy | Kumar | Lucknow | 71 | 82 | 69 | 71 | |
7 | Shyam | Roy | Sharma | Delhi | 85 | 90 | 68 | 78 |
85 | Abhay | Kumar | Gupta | Chandigarh | 45 | 68 | 82 | 78 |
509 | Riya | Roy | Sharma | Delhi | 68 | 90 | 69 | 91 |
65 | Vishal | Kumar | Sharma | Mumbai | 75 | 65 | 88 | 75 |
查询1: 下面的SELECT查询使用了上面Fresher_Marks表中的Fresher_ID列的CHAR函数:
SELECT Fresher_ID, First_Name, CHAR(Fresher_ID) AS ASCII_of_Fresher_ID FROM Fresher_Marks;
这个SQL语句显示了上表中每个新手的id的ASCII值:
输出:
Fresher_ID | First_Name | ASCII_of_Fresher_ID |
---|---|---|
501 | Vinay | NULL |
92 | Monu | \ |
104 | Ravi | h |
7 | Shyam | ^G |
85 | Abhay | U |
509 | Riya | NULL |
65 | Vishal | A |
查询2: 下面的SELECT查询使用了上面Fresher_Marks表的Aptitude_Marks列的CHAR函数:
SELECT Fresher_ID, Aptitude_Marks, CHAR(Aptitude_Marks) AS ASCII_of_ Aptitude_Marks FROM Fresher_Marks;
这个SQL语句显示了上表中每个新生的能力评分的ASCII值:
输出:
Fresher_ID | Aptitude_Marks | ASCII_of_Aptitude_Marks |
---|---|---|
501 | 85 | U |
92 | 54 | 6 (Digit) |
104 | 71 | G |
7 | 85 | U |
85 | 45 | - |
509 | 68 | D |
65 | 75 | K |
查询 3: 下面的SELECT查询使用了上述Fresher_Marks表的Reasoning_Marks和Technical_Marks列的CHAR函数:
SELECT Fresher_ID, Reasoning_Marks, CHAR(Reasoning_Marks) AS ASCII_of_ Reasoning_Marks , Technical_Marks, CHAR(Technical_Marks) AS ASCII_of_ Technical_Marks FROM Fresher_Marks;
这个SQL语句显示了上表中每个新人的推理和技术成绩的ASCII值:
输出:
Fresher_ID | Reasoning_Marks | ASCII_of_Reasoning_Marks | Technical_Marks | ASCII_of_Technical_Marks |
---|---|---|---|---|
501 | 92 | 6 (Digit) | 78 | N |
92 | 68 | D | 98 | b |
104 | 82 | R | 69 | E |
7 | 90 | Z | 68 | D |
85 | 68 | D | 82 | R |
509 | 90 | Z | 69 | E |
65 | 65 | A | 88 | X |
查询4: 下面的SELECT查询使用了上面Fresher_Marks表的Percentage列的CHAR函数:
SELECT Fresher_ID, Percentage, CHAR(Percentage) AS ASCII_of_ Percentage FROM Fresher_Marks;
输出:
Fresher_ID | Percentage | ASCII_of_Percentage |
---|---|---|
501 | 85 | U |
92 | 88 | X |
104 | 71 | G |
7 | 78 | N |
85 | 78 | N |
509 | 91 | [ |
65 | 75 | K |