?
Solved

SQL: select (x/y) as z

Posted on 2000-04-23
10
Medium Priority
?
622 Views
Last Modified: 2010-04-04
I wish the following query to return z as a real (x/y), not as an integer (x div y) which it does.  x and y are integer values of the table.

select (x/y) as z from table.db
where y > 0

what to do?

bobby

0
Comment
Question by:rwv
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 15

Expert Comment

by:simonet
ID: 2743019
Try this:

select CAST((x/y) as FLOAT(10,3)) as z from table.db
where y > 0

I can't remeber the syntax correctly, so if FLOAT(10,3) fails, try NUMBER(10,3), REAL, etc.

Alex
0
 

Author Comment

by:rwv
ID: 2743932
Alex, that didn't work, result was still an integer (or at least truncated)

I tried this and it works:

select (x+0.0)/y as z from table.db

To get 2 decimal places, this works:
select((x+100)/y)/100.0

However, the results look like
10
9.5
9.25
9
...etc.

Is there an easy way to format with out using the fields editor?
0
 
LVL 3

Accepted Solution

by:
shenqw earned 300 total points
ID: 2743933
select cast((x) as float(10,3))/cast((y) as float(10,3)) as z from table.db
  where y>0
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:simonet
ID: 2744027
>Is there an easy way to format without
>using the fields editor?

I don't think so. Using the fields editor gives you a consistent format throughout the application (wherever that particular TField is being used).

IMO, the Fields Editor is the easiest way to do it for calculated fields.

Alex
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 2746458
Hi,

Could you tell us which SQL Database you are using ?
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2746678
Why not just extract x and y as integers and then do the division (into a REAL) in your Delph code?

The Neil =:)
0
 

Author Comment

by:rwv
ID: 2747516
The Neil -
 I could do that, but then I would have to write the onDrawDataCell events for the dbgrid I want to display the results in, right?
0
 

Author Comment

by:rwv
ID: 2747526
Stefaan,
I'm using paradox tables.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2747625
rwv,

Good point. Didn't realise that you were outputting to a visual component - oops

The Neil =:(
0
 

Author Comment

by:rwv
ID: 2747880
error above should read:

To get 2 decimal places, this works:
select((x*100)/y)/100.0

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question