SQL Server - avg decimal places

Asked By rodchar
03-Feb-10 12:07 PM
hey all,

given:
avg(cast(myvalue as numeric(12,2)))

in SQL Management Studio I am getting:
4.3333

I thought it would be 4.33 because I defined 2 decimal places.

Any ideas?

thanks,
rodchar
Decimal
(1)
Avg
(1)
Attentionto
(1)
IDENTITY
(1)
Myvalue
(1)
Coward
(1)
ROGGIE
(1)
CLEKO
(1)
  Plamen Ratchev replied to rodchar
03-Feb-10 12:31 PM
This is because AVG of DECIMAL data type is treated as "DECIMAL(38, s) divided by DECIMAL(10, 0)":

http://msdn.microsoft.com/en-us/library/ms177677.aspx

You can cast the AVG result to desired precision:

SELECT AVG(CAST(x AS DECIMAL(12, 2))) AS avg_value
FROM (SELECT 1.231 UNION
SELECT 2.001) AS T(x);

/*

avg_value
---------------------------------------
1.615000

*/

SELECT CAST(AVG(x) AS DECIMAL(12, 2)) AS avg_value
FROM (SELECT 1.231 UNION
SELECT 2.001) AS T(x);

/*

avg_value
---------------------------------------
1.62

*/

--
Plamen Ratchev
http://www.SQLStudio.com
  rodchar replied to Plamen Ratchev
03-Feb-10 04:57 PM
thank you for the help,
rod.
  --CELKO-- replied to rodchar
03-Feb-10 09:20 PM
When you read the ANSI/ISO Standards, you saw the part of this being
implementation defined" of course, so you know this is a RTFM
question.
  m replied to --CELKO--
03-Feb-10 09:34 PM
Why would you expect that?  The average of a set of values can frequently be
finite and more precise than the input data.  Consider that the average of 2
& 3 is 2  1/2.  Since the size of the data set, in this case two, is an
exact value, and following propagation of error formulae, you can see that
there is a gain of significance in this case - this is common and expected.
The behaviour of SQL server is implementation specific, and may be hardware
dependent too (although nearly all platforms have now converged their FP &
BCD etc. behaviour).  If you are really interested in more detail, then
doing some research into numerical error theory will help tremendously.  If
not, then just cast the result to the desired precision and use it.
  Tony Rogerson replied to --CELKO--
04-Feb-10 02:02 AM
--CLEKO-- you ignorant FW (work it out).

Just because you are too lazy to read the product manual (we all know you have
not RTFM'd because you would  understand about IDENTITY, you would  understand about
YYYY-MM-DD inconsistency, you would  understand a hell of a lot more and save us
having to constantly correct your error filled posts).

You are a coward, bullying people online yet in the flesh would not say boo
to a goose - a coward of the highest degree.

--ROGGIE--
  Jay replied to Tony Rogerson
04-Feb-10 08:33 AM
He does it to get your attention, not for himself so much, as your attention
to the subject.

Have to admit it works, bizarre, but it does work.
Create New Account
help
format varchar value to decimal SQL Server Hi all, how can i format / convert a varchar value to decimal but with the decimal separator like that 45.1500 - -- -> 45.15 45.0000 - -- -> 45 45.10 - -- -> 45.1 45 thanks in advance SQL Server Programming Discussions CREATE TABLE (1) PRIMARY KEY (1) Varchar (1) Decimal (1) Dec (1) Convertable (1) Precedence (1) Istance (1) DECLARE @dec AS VARCHAR(50) SET @dec = '45.1500' SELECT CONVERT(DECIMAL(5, 2), @dec) You control decimal numbers by defining the scale of the decimal. Here is how to cast your example values: SELECT CAST('45.1500' AS DECIMAL(10, 2)), CAST('45.0000' AS DECIMAL(10, 0)), CAST('45.10' AS DECIMAL(10
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 getting the value as 0 can someone help please Thanks Shri SQL Server Programming Discussions Decimal (1) Declare (1) Blogster (1) Mariano (1) Gomez (1) Shri (1) Formcreator (1) Endperiod (1 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 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 is 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
are not null CREATE TABLE [dbo].[ReturnDecade]( [Id] [int] NOT NULL, [Decade] [int] NULL, [Mar0] [decimal](10, 4) NULL, [Jun0] [decimal](10, 4) NULL, [Sep0] [decimal](10, 4) NULL, [Dec0] [decimal](10, 4) NULL, [Mar1] [decimal](10, 4) NULL, [Jun1] [decimal](10, 4) NULL, [Sep1] [decimal](10, 4) NULL, [Dec1 decimal](10, 4) NULL, [Mar2] [decimal](10, 4) NULL, [Jun2] [decimal](10, 4) NULL, [Sep2] [decimal