[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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

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
Dovberman
Asked:
Dovberman
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
DovbermanAuthor Commented:
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
 
DovbermanAuthor Commented:
What expression would round the results to 4 decimal places.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now