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

Posted on 2007-08-04
Last Modified: 2010-03-20
Using a SQL Statement, I need to format a division result to 4 digits after the decimal point.


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 ?
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

    Author Comment

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

    Results are now


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


    Author Comment

    What expression would round the results to 4 decimal places.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]

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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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 ( 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…

    733 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

    17 Experts available now in Live!

    Get 1:1 Help Now