Published on

June 14, 2012

Understanding the SIGN Function in SQL Server

Have you ever wondered how the SIGN function works in SQL Server? In this blog post, we will explore this fundamental function and its usage in SQL Server.

The SIGN function is a simple yet powerful function that returns the value 1, -1, or 0 based on the input value. If the value is negative, it returns -1. If the value is positive, it returns 1. And if the value is zero, it returns 0.

Let’s start with a simple example to understand how the SIGN function works:

DECLARE @IntVal1 INT, @IntVal2 INT, @IntVal3 INT
DECLARE @NumVal1 DECIMAL(4, 2), @NumVal2 DECIMAL(4, 2), @NumVal3 DECIMAL(4, 2)

SET @IntVal1 = 9;
SET @IntVal2 = -9;
SET @IntVal3 = 0;

SET @NumVal1 = 9.0;
SET @NumVal2 = -9.0;
SET @NumVal3 = 0.0;

SELECT SIGN(@IntVal1) AS IntVal1, SIGN(@IntVal2) AS IntVal2, SIGN(@IntVal3) AS IntVal3
SELECT SIGN(@NumVal1) AS NumVal1, SIGN(@NumVal2) AS NumVal2, SIGN(@NumVal3) AS NumVal3

The above code will give us the following result set:

IntVal1IntVal2IntVal3
1-10
NumVal1NumVal2NumVal3
1-10

As you can see, when the input value is positive, the SIGN function returns a positive value. When the input value is negative, it returns a negative value. And when the input value is zero, it returns 0.

It’s important to note that the return type of the SIGN function depends on the data type of the input value. Here is a quick lookup of the return types:

  • bigint -> bigint
  • int/smallint/tinyint -> int
  • money/smallmoney -> money
  • numeric/decimal -> numeric/decimal
  • everybody else -> float

Now, let’s look at an example of how the SIGN function can be used to replace a CASE statement. Consider the following example:

USE tempdb

CREATE TABLE TestTable (
  Date1 SMALLDATETIME,
  Date2 SMALLDATETIME
)

INSERT INTO TestTable (Date1, Date2)
SELECT '2012-06-22 16:15', '2012-06-20 16:15'
UNION ALL
SELECT '2012-06-24 16:15', '2012-06-22 16:15'
UNION ALL
SELECT '2012-06-22 16:15', '2012-06-22 16:15'

-- Using Case Statement
SELECT CASE
  WHEN DATEDIFF(d, Date1, Date2) > 0 THEN 1
  WHEN DATEDIFF(d, Date1, Date2) < 0 THEN -1
  ELSE 0
END AS Col
FROM TestTable

-- Using SIGN Function
SELECT SIGN(DATEDIFF(d, Date1, Date2)) AS Col
FROM TestTable

DROP TABLE TestTable

In the above example, we have a table called TestTable with two columns, Date1 and Date2. We want to calculate the difference in days between the two dates and return 1 if Date2 is greater than Date1, -1 if Date2 is less than Date1, and 0 if they are equal.

Using a CASE statement, we can achieve this. However, the same result can be obtained using the SIGN function, as shown in the code above.

By using the SIGN function, we can simplify our code and make it more concise. This can be especially useful when dealing with complex queries or large datasets.

In conclusion, the SIGN function in SQL Server is a powerful tool that allows us to easily determine the sign of a value. It can be used to simplify our code and make it more readable. Understanding how this function works can greatly enhance our SQL Server development skills.

I hope you found this blog post informative. If you have any questions or comments, please let me know.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.