SQL SIGN函数
SQL中的SIGN字符串函数返回具有正负符号的指定数字。如果数字大于零,则函数返回1,否则返回-1。如果指定的数字为零,则函数返回结果为零。
SIGN字符串函数的语法
语法1: 此语法使用SQL表的列名与SIGN函数一起使用:
SELECT SIGN(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要在其上使用SIGN函数的列的名称。
Syntax2: 我们还可以使用SIGN函数与任何数字一起使用。
SELECT SIGN(Number) AS Alias_Name;
SIGN字符串函数示例
示例1: 以下SELECT查询使用SIGN函数0
SELECT SIGN(0)AS SIGN_zero;
输出:
SIGN_zero
---
0
示例 2: 以下SELECT查询显示了数字的符号
SELECT SIGN(+10) AS SIGN_positive;
输出:
SIGN_positive
---
1
示例3: 以下SELECT查询显示负数的符号:
SELECT SIGN(-0.5) AS SIGN_negative;
输出:
SIGN_negative
---
-1
示例4:本示例使用SQL中的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
);
下面是将有成绩和分数的学生记录插入到 Student_Percentage 表中的INSERT查询:
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语句显示了上述表中每个学生的roll number的 SIGN。
输出:
Roll_No | SIGN_RollNo |
---|---|
0 | 0 |
-2 | -1 |
-7 | -1 |
4 | 1 |
-11 | -1 |
16 | 1 |
-19 | -1 |
查询 2: 以下 SELECT 查询使用了上述 Student_Percentage 表中的 English_Marks 列的 SIGN 函数:
SELECT English_Marks, SIGN(English_Marks) AS SIGN_English FROM Student_Percentage;
这个SQL语句显示了每个学生英语成绩的符号。
输出:
English_Marks | SIGN_English |
---|---|
-10 | -1 |
-15 | -1 |
0 | 0 |
-15 | -1 |
5 | 1 |
0 | 0 |
2 | 1 |
查询 3: 以下 SELECT 查询在上面的 Student_Percentage 表中使用 Hindi_Marks 列的 SIGN 函数:
SELECT Hindi_Marks, SIGN(Hindi_Marks) AS SIGN_hindi FROM Student_Percentage;
这个SQL语句显示了每个学生的印地语成绩的符号。
输出:
Hindi_Marks | SIGN_hindi |
---|---|
88 | 1 |
-5 | -1 |
-1 | -1 |
85 | 1 |
-25 | -1 |
+95 | 1 |
89 | 1 |
查询4: 以下SELECT查询使用了上述Student_Percentage表中的Maths_Marks和Percentage列的SIGN函数:
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 |