Javatpoint Logo
Javatpoint Logo

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_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

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_No SIGN_RollNo
0 0
-2 -1
-7 -1
4 1
-11 -1
16 1
-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_Marks SIGN_English
-10 -1
-15 -1
0 0
-15 -1
5 1
0 0
2 1

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_Marks SIGN_hindi
88 1
-5 -1
-1 -1
85 1
-25 -1
+95 1
89 1

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_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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA