SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Scaled-down SQL


Scaled-down SQL

Author
Message
pksutha
pksutha
Valued Member
Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)

Group: General Forum Members
Points: 67 Visits: 58
My answer is like this
first part which is i 've answered:

Declare @value1 decimal(20,10),@value2 decimal(20,3)

SET @value1 = 1234567890.123456789
SET @value2 = 0.1

SELECT @value1 * @value2

Second part is screened answer:

DECLARE @value1 DECIMAL(20,10), @value2 DECIMAL(30,13)
SET @value1 = 1234567890.123456789
SET @value2 = 0.1

SELECT @value1 * @value2


Ans:
The screened answer is u 've declared value2 decimal(30,13)... why u need like that?
my answer is Declare @value1 decimal(20,10),@value2 decimal(20,3)...... this is enough in SQL server 2005
Duncan Pryde
Duncan Pryde
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4200 Visits: 1552
pksutha (3/22/2011)
My answer is like this
first part which is i 've answered:

Declare @value1 decimal(20,10),@value2 decimal(20,3)

SET @value1 = 1234567890.123456789
SET @value2 = 0.1

SELECT @value1 * @value2

Second part is screened answer:

DECLARE @value1 DECIMAL(20,10), @value2 DECIMAL(30,13)
SET @value1 = 1234567890.123456789
SET @value2 = 0.1

SELECT @value1 * @value2


Ans:
The screened answer is u 've declared value2 decimal(30,13)... why u need like that?
my answer is Declare @value1 decimal(20,10),@value2 decimal(20,3)...... this is enough in SQL server 2005


That's kind of the "moral" of the question. Never use a "bigger" decimal/numeric than you actually need.
michael.kaufmann
michael.kaufmann
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1591 Visits: 1082
Duncan Pryde (3/21/2011)
michael.kaufmann (3/21/2011)
tilew-948340 (3/20/2011)
[...]


[...]


Excellent explanation. Couldn't have put it better myself.


Thank you very much for your kind words of appreciation, Duncan.
Thanks again for your question and great explanation.

Regards,
Michael
Toreador
Toreador
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3871 Visits: 8128
Duncan Pryde (3/22/2011)
That's kind of the "moral" of the question. Never use a "bigger" decimal/numeric than you actually need.


The moral I've drawn is that maybe Floats aren't as bad as we thought...
rlswisher
rlswisher
SSC-Enthusiastic
SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)SSC-Enthusiastic (196 reputation)

Group: General Forum Members
Points: 196 Visits: 104
Declare @value1 numeric(38,10)
Declare @value2 numeric(1,1)
SET @value1 = 1234567890.123456789
SET @value2 = 0.1
SELECT @value1
SELECT @value2

SELECT @value1 * @value2
= "123456789.012345679"
Duncan Pryde
Duncan Pryde
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4200 Visits: 1552
rlswisher (3/22/2011)
Declare @value1 numeric(38,10)
Declare @value2 numeric(1,1)
SET @value1 = 1234567890.123456789
SET @value2 = 0.1
SELECT @value1
SELECT @value2

SELECT @value1 * @value2
= "123456789.012345679"


As expected.

Result precision is 38+1+1 = 40, scale is 10+1 = 11. Max allowed precision is 38, so precision and scale are reduced by 2, giving a final result precision and scale of 38,9 - which is why the result is rounded as you can see.
Duncan Pryde
Duncan Pryde
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4200 Visits: 1552
Toreador (3/22/2011)
Duncan Pryde (3/22/2011)
That's kind of the "moral" of the question. Never use a "bigger" decimal/numeric than you actually need.


The moral I've drawn is that maybe Floats aren't as bad as we thought...


Until you want to do something like this

Hehe
Gopi S
Gopi S
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 325
Excellent question and nice explanation..
Learned something new that Precision and Scale varies for the resulting value
based on (+, -, / , *, [UNION | EXCEPT | INTERSECT] , % ) .

Thanks for posting this...
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68499 Visits: 18570
great qeustion



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

SanDroid
SanDroid
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2482 Visits: 1046
Great question. You would not believe how long and how often programers get this wrong.
There is even a list that is maintained of ROM chips that do weight conversions incorrectly becuase Dec(6,2) is used instead of Dec(13,5)

I even had to "show the math" on this exact thing a year ago when I had to explain why the weight conversion code used to change Pound to Kilos and vice versa was wrong in every appliation where I work.

Not understanding this math is why so many ships have a problem balancing thier loads.
Sometimes the cargo is weighed in pounds and the balast program uses Kilos.
Then someone uses a cheap hand calculator to convert the 100,000 tons in pounds to Kilos and the weghts off by at least 1,000 tons.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search