SQL NCHAR函数
NCHAR字符串函数显示传入函数的整数的Unicode值。该函数只接受一个参数。如果我们传入的整数值超出了给定的范围,则显示NULL值。
NCHAR字符串函数的语法
在SQL中,我们可以使用NCHAR函数与表的列、字符串和字符一起使用。
语法1:
SELECT NCHAR(Integer_Column_Name) AS Alias_Name FROM Table_Name;
在这个语法中,我们使用了SQL的现有表来使用NCHAR函数。在这里,我们必须定义表的名称和整数列,我们希望对其进行NCHAR函数操作。
语法2:
SELECT NCHAR(Integer_Value) AS Alias_Name;
在这个语法中,我们使用了NCHAR函数和整数值。
NCHAR字符串函数的示例
示例1: 下面的SELECT查询显示了Unicode值为20:
SELECT NCHAR(20)AS Unicode_of_20;
输出:
Unicode_of_20
---
^T
示例2: 下面的SELECT查询显示了84的Unicode值:
SELECT NCHAR(98)AS Unicode_of_84;
输出:
Unicode_of_84
---
T
示例 3: 下面的SELECT查询显示Unicode值为260:
SELECT NCHAR(260)AS Unicode_of_260;
输出结果:
Unicode_of_98
---
NULL
示例4:此示例使用NCHAR函数与结构化查询语言中的表。
在这第四个示例中,我们将通过新建表来进行NCHAR函数与表值的操作:
下面的代码块展示了在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 VARNCHAR (100),
Middle_Name Varchar(120),
Last_Name VARNCHAR (200),
City Varchar(120),
Aptitude_Marks INT,
Reasoning_Marks INT,
Technical_Marks INT,
Percentage INT
);
以下是将新生的成绩和详细信息插入到Fresher_Marks表中的INSERT查询:
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查询使用了NCHAR函数和上述Fresher_Marks表中的Fresher_ID字段:
SELECT Fresher_ID, First_Name, NCHAR(Fresher_ID) AS Unicode_of_Fresher_ID FROM Fresher_Marks;
这个SQL语句展示了上述表中每个新人id的Unicode值:
输出
Fresher_ID | First_Name | Unicode_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列中使用了NCHAR函数:
SELECT Fresher_ID, Aptitude_Marks, NCHAR(Aptitude_Marks) AS Unicode_of_ Aptitude_Marks FROM Fresher_Marks;
该SQL语句显示了上表中每个新手的能力分数的Unicode值:
输出:
Fresher_ID | Aptitude_Marks | Unicode_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 列的 NCHAR 函数:
SELECT Fresher_ID, Reasoning_Marks, NCHAR(Reasoning_Marks) AS Unicode_of_ Reasoning_Marks , Technical_Marks, NCHAR(Technical_Marks) AS Unicode_of_ Technical_Marks FROM Fresher_Marks;
这个SQL语句显示了上表中每个新人的推理和技术分数的Unicode值:
输出:
Fresher_ID | Reasoning_Marks | Unicode_of_Reasoning_Marks | Technical_Marks | Unicode_of_Technical_Marks |
---|---|---|---|---|
501 | 92 | 6 (Digit) | 78 | N |
92 | 68 | D | 68 | 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列的NCHAR函数:
SELECT Fresher_ID, Percentage, NCHAR(Percentage) AS Unicode_of_ Percentage FROM Fresher_Marks;
输出:
Fresher_ID | Percentage | Unicode_of_Percentage |
---|---|---|
501 | 85 | U |
92 | 88 | X |
104 | 71 | G |
7 | 78 | N |
85 | 78 | N |
509 | 91 | [ |
65 | 75 | K |