Solved

SQL: select (x/y) as z

Posted on 2000-04-23
10
617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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
Industry Leaders: 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!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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