Math Functions – SQRT()

I am going to knuckle down and write another short article or tidbit on Transaction SQL math functions. Most, if not all, of these functions have been in the product before the release of SQL Server 2005.

I am very proud of my daughter making the advance math class in sixth grade. I am dedicating these blogs to her hard class work and love of the subject.

I will be exploring the SQRT() function today.

This function takes an expression of type float or of a type that can be implicitly converted to float. The output of the function is the mathematical square root. The square root of number X is a number Y such that Y * Y = X.

The example below tests the function with two different inputs: floating point number and a NULL value. It is interesting to note that the POWER() function can be used in place of the SQRT() function.

The output from the test is listed below.

Lets go back and review the definition of square root.

The square root of number X is a number Y such that Y * Y = X. If we set Y to -5, we end up with 25. On the other hand, if we set Y to 5, we end up with 25 again. Every positive number has two square roots. We always refer to the positive root as the result. So does the TSQL SQRT() function.

Last but not least, we can never have a number Y such that Y * Y that results in a negative result such as -X. To have a more comprehensive set of numbers, an imaginary unit i was introduced. It is defined as i * i = -1. This is a whole area of study called complex numbers.

The code snippet below demonstrates how the square root has both a negative and positive (root) solution. In addition, the TSQL function does not handle negative numbers that depend upon the imaginary unit. It generates an 3623 error message when passed a value less than zero.

The output from the test is listed below.

In summary, the SQRT function takes numbers greater than or equal to zero as input and returns the positive root as output.

Related posts

Leave a Comment