reset password
Author Message
victormejia
Posts: 40
Posted 20:52 Oct 20, 2010 |

I noticed if I do something like


select 1/2;

it gives me 0 and not 0.5. How can I get 0.5?

victormejia
Posts: 40
Posted 20:53 Oct 20, 2010 |

I also tried


select 4/3

and it gives 1. how do i get decimal precision?

cysun
Posts: 2935
Posted 21:48 Oct 20, 2010 |

It's integer division just like in Java. If you want fraction, change the type to real, double, float etc. You can do type cast, or just something like

select 1.0/2

Last edited by cysun at 21:48 Oct 20, 2010.
victormejia
Posts: 40
Posted 22:38 Oct 20, 2010 |

I see. But if I'm getting an integer returned from a subquery, how do I cast that to a double?

victormejia
Posts: 40
Posted 22:45 Oct 20, 2010 |

I tried

 

CAST ( expression AS type )


but that didn't work
cysun
Posts: 2935
Posted 08:11 Oct 21, 2010 |
victormejia wrote:

I tried

 

		CAST ( expression AS type )


but that didn't work

You probably didn't do it correctly. It should be something like

CAST ( count(*) as real )

Another simply way to do it is to multiply 1.0 to an integer:

1.0 * count(*)

rhardytran
Posts: 8
Posted 11:41 Oct 21, 2010 |

just simply multiply the result to 1.0 then divide it without doing type cast. For example,  result * 1.0 / some_number.  This should result double value.