Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 decimal places

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-03-05 : 15:27:47
How do i get these decimal working?


select
cast(2/5 as decimal(5,2))
,2/5
,2*5
,cast(5/2 as decimal (4,2))


thank you

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-03-05 : 15:32:34
[code]select
cast(2.0/5 as decimal(5,2))
,2.0/5
,2.0*5
,cast(5.0/2 as decimal (4,2))
[/code]
Go to Top of Page

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-03-05 : 15:33:18
Multiply each result by 1.0:
code[select 1.0 * 2 / 5,
1.0 * 2 / 5,
1.0 * 2 * 5,
1.0 * 5 / 2[/code]
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-03-05 : 15:39:06
nifty :) thank you very much

obviously

select
2/5*1.0
,2.0/5
,1.0*2/5

produces different results.
I was using the first one and it didn't had any effect whatsoever.


Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-03-05 : 15:40:07
p.s.: is there any logical explanation behind this? Anybody know?

select
2/5*1.0
,2.0/5
,1.0*2/5
Go to Top of Page

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-03-05 : 15:50:05
quote:
Originally posted by slimt_slimt

p.s.: is there any logical explanation behind this? Anybody know?

select
2/5*1.0
,2.0/5
,1.0*2/5




Its in the order of procedure. Your first equation performs the division (2/5) and then performs the multiplication.

If you were to specify that the multiplication is to take place before the division: 2/(5*1.0) you will get your expected result.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-03-05 : 15:56:18
The reason behind needing 1.0 is due to data type precedence.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -