April 30, 2009 at 12:02 pm
Hi guys, sorry to bother with such a simple question, but i dont know whats wrong on this simple code.
i keep getting Zero in the @D1/100 sections of the formula.
here's the little code
declare@D1 int, @D2 int, @D3 int, @D4 int, @D5 int, @TOTAL DECIMAL(10,2), @PRICE DECIMAL(10,2)
select @D1=30, @D2=5, @D3=2,@D4=0, @D5=0, @TOTAL=100.00,@PRICE=0
SET @PRICE = (@TOTAL *((1-(@D1/100))*
(1-(@D2/100))*
(1-(@D3/100))*
(1-(@D4/100))*
(1-(@D5/100))))
i just get @TOTAL multiplying 1's, because de divisions are cero, but its a simple 30/100 in the first one for example, why i get zero?
Am i missing something underground D:
if u do it directly on numbers it works as it should be
print 100*((1-.3)*(1-.05)*(1-.02)*(1-0)*(1-0))
txs in advance for any reply.
i feel like im gonna get punched in the face with soemthing so damn simple XD.
April 30, 2009 at 12:08 pm
it's integer division that is killing you
@integer / 100, if it is less than one, is zero. SQL server shortcuts the math and says int / int is an int result.
@integer / 100.00 is a decimal, which is what you want...just change your denominators to make sure they are decimal constants.
i get 65.17 with this formula:
declare @D1 int, @D2 int, @D3 int, @D4 int, @D5 int, @TOTAL DECIMAL(10,2), @PRICE DECIMAL(10,2)
select @D1=30, @D2=5, @D3=2,@D4=0, @D5=0, @TOTAL=100.00,@PRICE=0
SET @PRICE = (@TOTAL *((1-(@D1/100.0))*
(1-(@D2/100.0))*
(1-(@D3/100.0))*
(1-(@D4/100.0))*
(1-(@D5/100.0))))
print @PRICE
Lowell
April 30, 2009 at 12:23 pm
Oh you're right!
didn't know that implicit work of sql.
another new thing learned today.
Thanks a lot :w00t::w00t:
April 30, 2009 at 12:44 pm
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply