?
Solved

SQL: select (x/y) as z

Posted on 2000-04-23
10
Medium Priority
?
619 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 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
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!

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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