Open In App

SIGN() Function in SQL Server

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL Server, the SIGN() function is a mathematical function used to determine the sign of a given numeric expression. This function is particularly useful when we want to evaluate whether a number is positive, negative or zero. In this article, We will learn about SIGN() Function in SQL Server in detail by understanding various examples

SIGN() Function in SQL Server

  • The SIGN() function in SQL Server is used to return the sign of a given numeric expression.
  • It indicates whether the number is positive, negative or zero by returning 1, -1 or 0 respectively.

Features of SIGN() Function in SQL Server

  • This function is used to find the sign of the given number i.e., that given number is positive or negative or zero.
  • This function accepts only all types of numbers i.e., positive, negative, zero.
  • This function also accepts fraction numbers.
  • This function always returns 1 if the number is positive, -1 if the number is negative and 0 for zero number.

Syntax:

SIGN(number)

Parameter:

This method accepts a parameter as given below :

  • number: Specified number to return the sign for.

Returns:

It returns the sign of the specified number.

Examples of SIGN() Function in SQL Server

Example 1:

Getting the result as 1 i.e., positive for the specified number 2.

SELECT SIGN(2);

Output:

1

Example 2:

Getting the result as -1 i.e., negative for the specified number -7.

SELECT SIGN(-7);

Output:

-1

Example 3:

Let’s Determine the sign of a numeric variable in SQL Server that holds the value 0 using the SIGN() function.

DECLARE @Parameter_Value INT;
SET @Parameter_Value = 0;
SELECT SIGN(@Parameter_Value);

Output:

0

Explanation: In this query, a variable @Parameter_Value is declared as an integer and assigned a value of 0. The SIGN() function is then used to evaluate the sign of this variable. Since the value is 0, the SIGN() function will return 0, indicating that the number is neither positive nor negative.

Example 4:

Getting the result as 1 i.e., positive for the result of “5/2”.

SELECT SIGN(5/2);

Output:

1

Example 5:

Using SIGN() function with a variable and getting the result as 1 i.e., positive for the float value “5.75”.

DECLARE @Parameter_Value FLOAT;
SET @Parameter_Value = 5.75;
SELECT SIGN(@Parameter_Value);

Output:

1.0

Conclusion

The SIGN() function in SQL Server provides a simple yet powerful way to determine the sign of any numeric value, whether it’s positive, negative, or zero. This function is versatile and works with all numeric types, making it ideal for scenarios where you need to quickly evaluate or categorize numbers in SQL queries.



Next Article
Article Tags :

Similar Reads