I am going to remain on course with my series of short articles or tidbits 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 POWER() function today. This function takes two expressions, a base B and a exponent/power N, as input in either exact numeric or approximate numeric form. This does not include the BIT data type. If N is positive integer, the output of the function is the result of multiplying B times itself for a total of N times.
There is a huge area of mathematics study called exponentiation. We will talk about other types of exponents in the future.
The example below tests the POWER() function with five different pairs of inputs: exact numbers, approximate numbers, null base, null exponent, and null values for both arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-- -- The POWER Function -- -- Raise x to power of y SELECT POWER(2, 4) AS EXACT_POWER, POWER(2.500, 2.0) AS APROX_POWER, POWER(NULL, NULL) AS UNK_TEST1, POWER(2, NULL) AS UNK_TEST2, POWER(NULL, 4) AS UNK_TEST3 GO </span> |
The output from the test is listed below.
1 2 3 4 5 6 |
output: EXACT_POWER APROX_POWER UNK_TEST1 UNK_TEST2 UNK_TEST3 ------------ ------------ ---------- ---------- ---------- 16 6.250 NULL NULL NULL </span> |
In short, the POWER() function if a fine replace of the SQUARE() function if the exponent N, is the number 2.