Solved

SQL: select (x/y) as z

Posted on 2000-04-23
10
609 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

20 Experts available now in Live!

Get 1:1 Help Now