Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:44:21+00:00 2026-06-03T01:44:21+00:00

I’m trying to divide some integers to get a percentage. By default I get

  • 0

I’m trying to divide some integers to get a percentage. By default I get an integer result, of course, so I cast to decimal. But I also would like the result to only have two places to the right of the decimal.

--select (x - y) / y

select (82 - 56) / 56                                      --0
select (82 - 56) / cast(56 as decimal(9, 2))               --0.4642857142
select cast(82 - 56 as decimal(9,2)) / 56                  --0.464285
select cast((82 - 56) / 56 as decimal(9,2))                --0.00

select cast(cast(82 - 56 as decimal) / 56 as decimal(9,2)) --.46

So I end up having to cast both one of the operands and the entire expression in order to get the result I want. Is there a better way to do this?

Notice I put the equation up top to signify that I am obviously not working with hardcoded numbers (i.e. Suggesting I just put “.0” after the numbers would be less than helpful). Also, I would normally leave the formatting aspect to reporting software, but this is being copied from time to time into an excel spreadsheet and I would have to hand format it every time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:44:23+00:00Added an answer on June 3, 2026 at 1:44 am

    When dividing a DECIMAL by an INTEGER, SQL Server yields a DECIMAL with precision defined as p1 – s1 + s2 + max(6, s1 + p2 + 1), where sn and pn are the scale and the precision of the 1st and 2nd arguments (the divident and the divisor).

    The simplest way would be:

    SELECT  CAST(1. * (x - y) / y AS DECIMAL(9, 2))
    

    1. is a DECIMAL(1, 0) which, being multiplied by an integer, yields a DECIMAL(n, 0). Being divided by an integer, it would then yield a DECIMAL(n, 6) (for the number orders similar to those in your example).

    It then needs to be cast to DECIMAL(9, 2) to truncate the extra digits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.