PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > subtraction of different precision values
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
subtraction of different precision values

Réponse
 
LinkBack Outils de la discussion
Vieux 24/09/2007, 19h17   #1
ibcarolek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut subtraction of different precision values

We have a field which is decimal (9,2) and another which is decimal
(9,3). Is there anyway to subtract the two and get a precision 3
value without changing the first field to 9,3?

For instance, retail value is 9,2, but our costs are at 9,3 due to
being averaged. To calculate margin (retail-cost), we want that also
to be 9,3, but a basic subtraction comes out 9,2. You can see we
don't want to increase retail to be 9,3 (that would look funny), and
it seems wasteful to store retail twice (one 9,2 for users and one 9,3
for margin calc)...is there any other way?

  Réponse avec citation
Vieux 24/09/2007, 19h48   #2
Hugo Kornelis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: subtraction of different precision values

On Mon, 24 Sep 2007 10:17:15 -0700, ibcarolek wrote:

>We have a field which is decimal (9,2) and another which is decimal
>(9,3). Is there anyway to subtract the two and get a precision 3
>value without changing the first field to 9,3?
>
>For instance, retail value is 9,2, but our costs are at 9,3 due to
>being averaged. To calculate margin (retail-cost), we want that also
>to be 9,3, but a basic subtraction comes out 9,2. You can see we
>don't want to increase retail to be 9,3 (that would look funny), and
>it seems wasteful to store retail twice (one 9,2 for users and one 9,3
>for margin calc)...is there any other way?


Hi ibcarolek,

Can you post a repro that demonstrates the issue? If I subtract a
decimal(9,3) from a decimal(9,2), the result has three decimal places,
as demonstrated by the repro below. You are obviously doing something in
a different way than I am - I need to know what before I can you
solve the issue.

DECLARE @Retail decimal(9,2), @Costs decimal(9,3);
SET @Retail = 12.04;
SET @Costs = 9.833;
SELECT @Retail - @Costs;

Result:

2.207


--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
  Réponse avec citation
Vieux 24/09/2007, 19h50   #3
Roy Harvey (SQL Server MVP)
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: subtraction of different precision values

On Mon, 24 Sep 2007 10:17:15 -0700, ibcarolek <carolek@ix.netcom.com>
wrote:

>We have a field which is decimal (9,2) and another which is decimal
>(9,3). Is there anyway to subtract the two and get a precision 3
>value without changing the first field to 9,3?
>
>For instance, retail value is 9,2, but our costs are at 9,3 due to
>being averaged. To calculate margin (retail-cost), we want that also
>to be 9,3, but a basic subtraction comes out 9,2. You can see we
>don't want to increase retail to be 9,3 (that would look funny), and
>it seems wasteful to store retail twice (one 9,2 for users and one 9,3
>for margin calc)...is there any other way?


I ran the following test using SQL Server 2000 and 2005 but could not
reproduce the behavior you describe.

DECLARE @field1 decimal(9,2)
SET @field1 = 123.45
DECLARE @field2 decimal(9,3)
SET @field2 = 123.456

SELECT @field1, @field2, @field1 - @field2

----------- ----------- -------------
123.45 123.456 -.006

The result always went to three decimal places.

Having said that, you can explicitly convert before performing the
calculation:

SELECT CONVERT(decimal(9,3),@field1) - @field2
SELECT CAST (@field1 as decimal(9,3)) - @field2

Roy Harvey
Beacon Falls, CT
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10151 seconds with 11 queries