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
 Getting fractions

Author  Topic 

dmilam
Posting Yak Master

185 Posts

Posted - 2011-02-21 : 17:52:42
[code]
select cast((2/3) as numeric(10,5))
[/code]

Why does this not return 0.66666 ?

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-02-21 : 18:28:49
Integer math is done first, then conversion. Duh!

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2011-02-21 : 18:40:41
I prefer an example, no duh.


declare @a numeric(2,1), @b numeric(2,1)
set @a = 2
set @b = 3
select @a / @b


Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-02-22 : 03:17:42
select cast((2/3.0) as numeric(10,5))

--Ranjit
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2011-02-22 : 05:24:56
select 2/3.0

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2011-02-22 : 12:48:21
Thank you for the examples.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-24 : 03:16:30
Also refer this
http://beyondrelational.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -