Solved

SQL: select (x/y) as z

Posted on 2000-04-23
10
610 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now