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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Why aren't I seeing the number that I expect?

Author  Topic 

Darts75
Starting Member

27 Posts

Posted - 2014-09-05 : 01:08:12
Hi Everyone,

I don't understand why I am getting all zeros after the decimal point below.



I have two main questions -

1). Why are there so many places after the decimal point?
2). Why am I seeing nothing but zeros (I expected 14.46888137723487)?

Any help will be greatly appreciated.

Kind Regards,

David

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-09-05 : 01:16:12
few points
1. you declare the variable as decimal without specifying the precision and scale. I can't remember what is the default off hand. Do check the BOL
2. when assigning value to decimal, you does not need to enclosed it in single quote. Doing so will means you are assigning as string '105.1234' to a decimal variable and sql server will do an implicit conversion
3. do a select @dividend, @divisor and see what is the value after your SET statement



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Darts75
Starting Member

27 Posts

Posted - 2014-09-05 : 02:03:55
Hi khtan,

Thank you for your response. Initially when I tried to specify the decimal values parameters, e.g.: DECLARE @dividend decimal(10,3), I was met with 'red underlines' in SSMS. However something unrelated was probably happening.

At any rate now that I am able to correctly setup the variables I am able to lead into the real issue that I am facing. I really want to limit the decimal 'fidelity' to two decimal places after a division operation, however no matter what I do I can't seem to achieve the desired outcomes.



What is happening above?

Why after declaring decimal(10,3) am I seeing so many decimal places in my result?

Why is a CAST statement that specifically tells both the dividend and the divisor to be two decimal places being ignored?

How can I get a result like 14.59?

Adding a CAST to the entire line, e.g.: CAST(@dividend / @divisor AS decimal(15,2)), causes an 'Incorrect Syntax' error!

Any further help or suggestions will be greatly appreciated.

Kind Regards,

David
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-09-05 : 02:08:22
cast if after the calculation


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

shantheguy
Starting Member

22 Posts

Posted - 2014-09-09 : 05:52:05
declare @div decimal(10,3),@dev decimal(10,3)

set @div=105.12345425
set @dev=7.2654859425

print @div

select cast(@div/@dev as decimal(10,3))

Try this
Go to Top of Page
   

- Advertisement -