Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / December 2008

Tip: Looking for answers? Try searching our database.

Calculate Power

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
formcreator - 01 Dec 2008 21:29 GMT
Hello All
I am trying to perform the following calculation

100^(-.01*25)

Here is the code I have right now

declare @decay_rate decimal;
declare @decay_days decimal;
declare @decay_calc decimal;
declare @power decimal;
set @decay_rate = 0.01

if(datediff(d,@endperiod,getdate()) > 5)
begin
set @decay_days = datediff(d,@endperiod,getdate())
set @power=(@decay_days*@decay_rate)
set @decay_calc = (power(100,(@power)))
--=(100^((B6*B7)))*100

What am I doing wrong here?

I keep getting the value as 0 can someone help please

Thanks
Shri
Mariano Gomez - 01 Dec 2008 21:41 GMT
The value 100 is casting the expression to integer, replace as follows:

set @decay_calc = power(100.00000, @power)

I am not sure what level of precision you are looking for, but you can
reduce/increase the decimals after 100 as you see fit, or simply store the
value 100 in a decimal variable as well.

Best regards,
--
MG.-
Mariano Gomez, MIS, MCP, PMP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com
The Dynamics GP Blogster at http://dynamicsgpblogster.blogspot.com

> Hello All
> I am trying to perform the following calculation
[quoted text clipped - 22 lines]
> Thanks
> Shri
Plamen Ratchev - 01 Dec 2008 21:48 GMT
You declared all of your variables as DECIMAL without specifying
precision and scale. When you do not define them the default precision
is 18 and the default scale is 0. That converts the value 0.01 to 0.

Try this:

DECLARE @decay_rate DECIMAL;
SET @decay_rate = 0.01;
SELECT @decay_rate;

/* returns 0 */

GO

DECLARE @decay_rate DECIMAL(18, 2);
SET @decay_rate = 0.01;
SELECT @decay_rate;

/* returns 0.01 */

Declare all variables with correct precision and scale and your
calculation should work.

Signature

Plamen Ratchev
http://www.SQLStudio.com

Mariano Gomez - 01 Dec 2008 22:15 GMT
That's correct! I did not notice this to begin with!

Best regards,
--
MG.-
Mariano Gomez, MIS, MCP, PMP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com
The Dynamics GP Blogster at http://dynamicsgpblogster.blogspot.com

> You declared all of your variables as DECIMAL without specifying
> precision and scale. When you do not define them the default precision
[quoted text clipped - 18 lines]
> Declare all variables with correct precision and scale and your
> calculation should work.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.