POWER (Transact-SQL)
Returns a number raised to a specified power.
Syntax
POWER ( base, exponent )
Parameters
| Parameter | Description | Type |
|---|---|---|
base |
The expression that is the base. Can be of any of the numeric data types. | numeric_expression |
exponent |
The expression that is the exponent. Can be of any of the numeric data types. | numeric_expression |
Return Type
Returns the same data type as the input base parameter, unless the base is a small data type and the result requires a larger data type. In that case, it returns the smallest data type that can hold the result.
Examples
Example 1: Basic Usage
Calculates 2 raised to the power of 3.
SELECT POWER(2, 3);
-- Result: 8
Example 2: Using Decimal Numbers
Calculates 3.5 raised to the power of 2.5.
SELECT POWER(3.5, 2.5);
-- Result: 20.500957526168529676625469139641
Example 3: Negative Exponent
Calculates 10 raised to the power of -2 (which is 1/100).
SELECT POWER(10, -2);
-- Result: 0.010000000000000000000000000000000
Example 4: With Table Data
Calculates a discount factor for each product's price.
-- Assuming a table named Products with columns ProductID and Price
-- SELECT
-- ProductID,
-- Price,
-- POWER(Price, 0.9) AS DiscountedPrice
-- FROM
-- Products;
Note: The example above is commented out. In a real scenario, you would uncomment and adapt it to your table structure.
Remarks
- If either argument is
NULL,POWERreturnsNULL. - If the exponent is 0,
POWERreturns 1. - If the exponent is negative,
POWERreturns 0. - The function returns
NULLif an attempt is made to calculate a result that is outside the range of the return type. - For better performance, use the most precise data type that is appropriate for the input.