SQL CHAR 函数

一、SQL CHAR 函数 语法

CHAR 字符串函数显示函数中传递的整数的 ASCII 值。这个函数只接受一个参数。如果我们传递超出给定范围的整数值,则它显示 NULL 值。

在 SQL 中,我们可以对表的列、字符串和字符使用 CHAR 函数。

语法 1

SELECT CHAR(Integer_Column_Name) AS Alias_Name FROM Table_Name;

在这种语法中,我们将 CHAR 函数与现有的 SQL 表一起使用。在这里,我们必须定义要在其上执行 CHAR 函数的表的名称和整数列。

语法 2

SELECT CHAR(Integer_Value) AS Alias_Name;  

在此语法中,我们使用带有整数值的 CHAR 函数。

二、SQL CHAR 函数 示例

示例 1:以下 SELECT 查询显示 ASCII 值 20:

SELECT CHAR(20)AS ASCII_of_20;

输出结果为:

ASCII_of_20
^T

示例 2:以下 SELECT 查询显示 ASCII 值 84:

SELECT CHAR(98)AS ASCII_of_84;  

输出结果为:

ASCII_of_84
T

示例 3:以下 SELECT 查询显示 ASCII 值 260:

SELECT CHAR(260)AS ASCII_of_260;  

输出结果为:

ASCII_of_98
NULL

示例 4:此示例对结构化查询语言中的表使用 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 查询将 CHAR 函数与上述 Fresher_Marks 表的 Fresher_ID 列一起使用:

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 查询将 CHAR 函数与上述 Fresher_Marks 表的 Aptitude_Marks 列一起使用:

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 查询将 CHAR 函数与上述 Fresher_Marks 表的 Reasoning_Marks 和 Technical_Marks 列一起使用:

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 查询将 CHAR 函数与上述 Fresher_Marks 表的 Percentage 列一起使用:

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

热门文章

优秀文章