SQL SIGN 函数
一、SQL SIGN 函数 语法
SQL语言中的 SIGN函数 返回带有正号或负号的指定数字。如果数字大于零,则函数返回 1,否则返回 -1。如果指定的数字为零,则函数在结果中返回零。
语法1:此语法使用带有 SQL 表列名的 SIGN 函数:
SELECT SIGN(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定必须使用 SIGN 函数的列的名称
语法 2:我们也可以对任意数字使用 SIGN 函数:
SELECT SIGN(Number) AS Alias_Name;
二、SQL SIGN 函数 示例
示例 1:以下 SELECT 查询使用 SIGN 函数 0
SELECT SIGN(0)AS SIGN_zero;
输出结果为:
SIGN_zero |
---|
0 |
示例 2:以下 SELECT 查询显示数字的 SIGN
SELECT SIGN(+10) AS SIGN_positive;
输出结果为:
SIGN_positive |
---|
1 |
示例 3:以下 SELECT 查询显示负数的 SIGN:
SELECT SIGN(-0.5) AS SIGN_negative;
输出结果为:
SIGN_negative |
---|
-1 |
示例 4:此示例对结构化查询语言中的表使用 SIGN 函数。
在 SQL 数据库中创建新表的语法如下:
CREATE TABLE table_name
(
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 语句创建Student_Percentage表:
CREATE TABLE Student_Percentage
(
Roll_No INT PRIMARY KEY,
First_Name VARCHAR (100),
Last_Name VARCHAR (100),
First_CityVarchar(120),
Second_CityVarchar(120),
English_Marks INT,
Hindi_Marks INT,
Maths_Marks INT,
Percentage INT
);
下面的 INSERT 语句在Student_Percentage表中插入学生的成绩和分数记录:
INSERT INTO Student_Percentage (Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage) VALUES (10, Aman, Sharma, Lucknow Chandigarh, -10, 88, 0, 98.8);
INSERT INTO Student_Percentage
(Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage)
VALUES ( -2, Vishal, Sharma, Chandigarh, Ghaziabad, -15, 5, 0, -15.5 );
INSERT INTO Student_Percentage
(Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage)
VALUES (-7, Raj, Gupta, Delhi, Ghaziabad, 0, -1, 95, 80);
INSERT INTO Student_Percentage
(Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage)
VALUES (4, Yash, Singhania, Ghaziabad, Delhi, -15, 85, -82, -70.2);
INSERT INTO Student_Percentage
(Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage)
VALUES (-11, Vinay, Roy, Delhi, Kanpur, 5, -25, 0, -20.8);
INSERT INTO Student_Percentage
(Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage)
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, 0, 95, -90, 55);
INSERT INTO Student_Percentage
(Roll_No, First_Name, Last_Name,First_City, Second_City, English_Marks, Hindi_Marks, Maths_Marks, Percentage)
VALUES (-19, Ram, Gupta, Lucknow, Ghaziabad, 2, 89, 95, 80);
以下 SELECT 语句显示上述Student_Percentage表的插入记录:
SELECT * FROM Student_Percentage;
输出结果为:
Roll_No | First_Name | Last_Name | First_City | Second_City | English_Marks | Hindi_Marks | Maths_Marks | Percentage |
---|---|---|---|---|---|---|---|---|
0 | Aman | Sharma | Lucknow | Chandigarh | -10 | 88 | 0 | 98.8 |
-2 | Vishal | Sharma | Chandigarh | Ghaziabad | -15 | -5 | 0 | -15.5 |
-7 | Raj | Gupta | Delhi | Ghaziabad | 0 | -1 | 95 | 80 |
4 | Yash | Singhania | Ghaziabad | Delhi | -15 | 85 | -82 | -70.2 |
-11 | Vinay | Roy | Delhi | Kanpur | 5 | -25 | 0 | -20.8 |
16 | Manoj | Gupta | Ghaziabad | Meerut | 0 | +95 | -90 | 55 |
-19 | Ram | Gupta | Lucknow | Ghaziabad | 2 | 89 | 95 | 80 |
查询 1:以下 SELECT 查询使用带有上述 Student_Percentage 表的 Roll_No 列的 SIGN 函数:
SELECT Roll_No, SIGN(Roll_No) AS SIGN_RollNo FROM Student_Percentage;
此 SQL 语句显示上表中每个学生的卷号 SIGN。
输出结果为:
Roll_No | SIGN_RollNo |
---|---|
0 | 0 |
-2 | -1 |
-7 | -1 |
4 | 1 |
-11 | -1 |
16 | 1 |
-19 | -1 |
查询 2:以下 SELECT 查询将 SIGN 函数与上述 Student_Percentage 表的 English_Marks 列一起使用:
SELECT English_Marks, SIGN(English_Marks) AS SIGN_English FROM Student_Percentage;
该 SQL 语句显示每个学生的英语成绩的 SIGN。
输出结果为:
English_Marks | SIGN_English |
---|---|
-10 | -1 |
-15 | -1 |
0 | 0 |
-15 | -1 |
5 | 1 |
0 | 0 |
2 | 1 |
查询 3:以下 SELECT 查询将 SIGN 函数与上述 Student_Percentage 表的 Hindi_Marks 列一起使用:
SELECT Hindi_Marks, SIGN(Hindi_Marks) AS SIGN_hindi FROM Student_Percentage;
此 SQL 语句显示每个学生的印地语标记的 SIGN。
输出结果为:
Hindi_Marks | SIGN_hindi |
---|---|
88 | 1 |
-5 | -1 |
-1 | -1 |
85 | 1 |
-25 | -1 |
+95 | 1 |
89 | 1 |
查询 4:以下 SELECT 查询将 SIGN 函数与上述 Student_Percentage 表的 Maths_Marks 和 Percentage 列一起使用:
SELECT Maths_Marks, SIGN(Maths_Marks), Percentage, SIGN(Percentage) FROM Student_Percentage;
输出结果为:
Maths_Marks | SIGN( Maths_Marks) | Percentage | SIGN( Percentage) |
---|---|---|---|
0 | 0 | 98.8 | 1 |
0 | 0 | -15.5 | -1 |
95 | 1 | 80 | 1 |
-82 | -1 | -70.2 | -1 |
0 | 0 | -20.8 | -1 |
-90 | -1 | 55 | 1 |
95 | 1 | 80 | 1 |
热门文章
优秀文章