SIGN Function in SQL

The SIGN string function in Structured Query Language returns the specified number with the positive or negative sign. If the number is greater than zero, the function returns 1 otherwise -1. If the number specified as zero, the function returns zero in result.

Syntax of SIGN String Function

Syntax1: This syntax uses the SIGN function with the column name of the SQL table:

In the syntax, we have to specify the name of that column on which we have to use SIGN function

Syntax2: We can also use the SIGN function with any number:

Examples of SIGN String function

Example 1: The following SELECT query uses the SIGN function 0

Output:

SIGN_zero
0

Example 2: The following SELECT query shows the SIGN of the number

Output:

SIGN_positive
1

Example 3: following SELECT query shows the SIGN of the negative number:

Output:

SIGN_negative
-1

Example 4: This example uses the SIGN function with the table in Structured Query Language.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Percentage table:

The below INSERT queries insert the records of students with grades and marks in the Student_Percentage table:

The following SELECT statementdisplays the inserted records of the above Student_Percentage table:


Roll_NoFirst_NameLast_NameFirst_CitySecond_CityEnglish_MarksHindi_MarksMaths_MarksPercentage
0AmanSharmaLucknowChandigarh-1088098.8
-2VishalSharmaChandigarhGhaziabad-15-50-15.5
-7RajGuptaDelhiGhaziabad0-19580
4YashSinghaniaGhaziabadDelhi-1585-82-70.2
-11VinayRoyDelhiKanpur5-250-20.8
16ManojGuptaGhaziabadMeerut0+95-9055
-19RamGuptaLucknowGhaziabad2899580

Query 1: The following SELECT query uses the SIGN function with the Roll_No column of the above Student_Percentage table:

This SQL statement shows the SIGN of roll number of each student from the above table.

Output:

Roll_NoSIGN_RollNo
00
-2-1
-7-1
41
-11-1
161
-19-1

Query 2: The following SELECT query uses the SIGN function with the English_Marks column of the above Student_Percentage table:

This SQL statement shows the SIGN of the english marks of each student.

Output:

English_MarksSIGN_English
-10-1
-15-1
00
-15-1
51
00
21

Query 3: The following SELECT query uses the SIGN function with the Hindi_Marks column of the above Student_Percentage table:

This SQL statement shows the SIGN of the hindi marks of each student.

Output:

Hindi_MarksSIGN_hindi
881
-5-1
-1-1
851
-25-1
+951
891

Query 4: The following SELECT query uses the SIGN function with the Maths_Marks and Percentage column of the above Student_Percentage table:

Output:

Maths_MarksSIGN( Maths_Marks)PercentageSIGN( Percentage)
0098.81
00-15.5-1
951801
-82-1-70.2-1
00-20.8-1
-90-1551
951801





Latest Courses