Solved

# Using a SQL Statement, I need to format a division result to 4 digits after the decimal point.

Posted on 2007-08-04
244 Views
Using a SQL Statement, I need to format a division result to 4 digits after the decimal point.

Example:

SELECT X, Y, X/Y As Ratio FROM Filename
X and Y are stored as Real.

X=40.12345678, Y=38.2727272

Query returns 1 for X/Y

Should return 1.048

What expression should I use ?
0
Question by:Dovberman

LVL 142

Accepted Solution

real? you should use decimal(20,8) instead, for example.

SELECT X, Y, cast(X as decimal(20,8)) / cast(Y as decimal(20,8)) As Ratio FROM Filename
0

Author Comment

My mistake. I was using X/X instead of X/Y.

Results are now

1.075179
1.103112
1.054205
1.106358
1.038615

I would still like to round results only to 4 decimal places.

Thanks,
0

Author Comment

What expression would round the results to 4 decimal places.
0

LVL 142

Expert Comment

SELECT X, Y, cast( X / Y  as decimal(20,4)) As Ratio FROM Filename
0

## Featured Post

### Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…