Home > T-SQL > SQL SERVER – POWER() – SQL Math Functions @SQLSERVER

SQL SERVER – POWER() – SQL Math Functions @SQLSERVER


Power() function is used to calculate xn  value. The return type of this function is same as float expression type.

Syntax:

Power(float expression, power value)

Example

select power(10, 3)
Output: 1000
 
select Power(-12, 2)
Output: 144
 
select power(2, 0)
Output: 1
 
select Power(1.3, 6)
Output: 4.8
 
T-SQL code to print a series of value for 2n
 
declare @i as int = 1
declare @n as int = 10
declare @x as float = 2.0
 
while @i <= @n
begin
print  ‘Power(‘+ cast(@x as varchar(3)) + ‘, ‘+ cast(@i as varchar(2)) + ‘) = ‘
print power(@x, @i)
set @i = @i +1
end
 
Output:
Power(2, 1) =
2
Power(2, 2) =
4
Power(2, 3) =
8
Power(2, 4) =
16
Power(2, 5) =
32
Power(2, 6) =
64
Power(2, 7) =
128
Power(2, 8) =
256
Power(2, 9) =
512
Power(2, 10) =
1024
 

So, we learned to compute x^n using T-SQL POWER() function today. Thanks for reading and I will meet you with another math function tomorrow.

 
Advertisements
  1. No comments yet.
  1. December 25, 2012 at 11:37 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: