Open In App

SQL Server POWER() Function

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

The POWER() function in SQL Server is a mathematical function that computes the result of raising a number (the base) to the power of another number (the exponent). It is a versatile function used for various calculations, such as squaring a number, computing roots or applying exponential growth in data analysis.

In this article, We will learn about SQL Server POWER() Function in detail with the help of various examples and so on.

SQL Server POWER() Function

  • The POWER() function in SQL Server is used to raise a number to a specified power.
  • It takes two arguments: a base number and an exponent and it returns the base number raised to the power of the exponent

For example, if the base is 5 and the exponent is 2, this will return a result of 25.

Syntax:

The POWER function syntax is:

POWER(base, exponent);

Parameter

This method accepts two parameters, as given below:

  • Base: Specified base number.
  • Exponent: Specified exponent number.

The base number can be positive or negative, while the exponent must be a positive number.

Examples of SQL Server POWER() Function

Let’s look at some examples of the POWER() function in SQL Server.

Example 1:

In this example, we will return a result of 49 for the base value 7 and the exponent value 2.

Query:

SELECT POWER(7, 2);

Output :

49

Explantion: This query calculates \(7^2\) using the `POWER()` function, which results in `49`. It raises the number `7` to the power of `2`, yielding the square of `7`.

Example 2:

In this example, we are using POWER() function with variables and getting a result of 1 for the base value 6 and exponent value 0.

Query:

DECLARE @Base_Value INT;
DECLARE @Exponent_Value INT;
SET @Base_Value = 6;
SET @Exponent_Value = 0;
SELECT POWER(@Base_Value, @Exponent_Value);

Output :

1

Explantion: This query declares two integer variables, `@Base_Value` and `@Exponent_Value`, and sets their values to `6` and `0`, respectively. It then calculates \(6^0\) using the `POWER()` function, which results in `1`, since any number raised to the power of `0` is `1`.

Example 3:

In this example, we will get a result of 0 for the base value 0 and exponent value 4.

SELECT POWER(0, 4);

Output :

0

Explantion: This query calculates \(0^4\) using the `POWER()` function, which results in `0`. Raising zero to any positive power always yields zero.

Example 4:

In this example, we will get a result of -64 for the base value -4 and exponent value 3.

SELECT POWER(-4, 3);

Output:

-64

Explanation: This query calculates (−4)3(-4)^3(−4)3 using the POWER() function, which results in -64. It raises the number -4 to the power of 3, producing the cube of -4, which is -64 because raising a negative number to an odd power yields a negative result.

Conclusion

The POWER() function is a powerful tool in SQL Server for performing exponential calculations. It handles both positive and negative base numbers and allows for flexibility with different exponents. By understanding how to use this function effectively, you can simplify complex mathematical operations and enhance your data analysis capabilities within SQL Server.



Next Article
Article Tags :

Similar Reads