SQL Server POWER() Function
Last Updated :
06 Sep, 2024
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.
Similar Reads
SQL Server LEN() Function
SQL SERVER LEN() function calculates the number of characters of an input string, excluding the trailing spaces. LEN() Function in SQL ServerThe LEN function in the SQL Server fetches the number of characters in a string. It counts the preceding spaces but not the trailing spaces. For eg, 'SQL SERVE
2 min read
PI() Function in SQL Server
PI() function : This function in SQL Server is used to return the constant float value of math Pi. The default number of decimal places displayed is seven, but SQL Server uses the full double-precision value internally. Features : This function is used to get the value of pi.This function does not a
1 min read
SQL Server ROUND() Function
The SQL Server ROUND() function rounds off a specified number to a decimal place. If the length is negative and larger than the number of digits before the decimal point, ROUND returns 0. ROUND in SQL ServerThe ROUND() function in SQL Server rounds off a number to a specified decimal place. It accep
2 min read
LOG() Function in SQL Server
The LOG() function returns the logarithm of a specified number or the logarithm of the number to the specified base. Syntax : LOG(number, base) Parameter : LOG() function accepts two-parameters as mentioned above and described below. number - This parameter hold a number which is greater than 0. bas
1 min read
SQL Server Group Functions
The group function in SQL Server provides a powerful tool for performing calculations on groups of rows, allowing you to group data based on specific criteria. This function is important when you want to analyze and summarize information from multiple records in a data structure. The basic group fun
3 min read
SQL Server GETDATE() Function
SQL Server GETDATE() function returns the current date and time of the database system in a 'YYYY-MM-DD hh:mm:ss.mmm' format. Note: GETDATE() is similar to other date and time functions like CURRENT_TIMESTAMP, SYSDATETIME(), and GETUTCDATE(), but differs primarily in terms of the time zone and preci
2 min read
RANK() Function in SQL Server
The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a result set. It is particularly useful when we need to assign a rank to a group of rows based on some sorting criteria and want to differentiate between rows that have the same values. Unlike ot
5 min read
LOG10() Function in SQL Server
This function returns the logarithm of a number to base 10. Syntax : LOG10(number) Parameter : This method accepts a single-parameter as mentioned above and described below. number - This parameter hold a number which is greater than 0. Returns - It returns the base-10 logarithm of the specified num
1 min read
MONTH() Function in SQL Server
MONTH() function : This function in SQL Server is used to return the month of the year i.e, from 1 to 12 for a date stated. Features : This function is used to find the month of the year for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, dat
2 min read
NCHAR() Function in SQL Server
NCHAR() function : This function in SQL Server is used to return the Unicode character that is based on the number code. For example, if the specified number is 65 then this function will return A. Features : This function is used to find the Unicode character of a given number. This function accept
2 min read