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

  • SEARCH
  • Home
  • 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 8177449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:30:48+00:00 2026-06-06T23:30:48+00:00

I have a section of T-SQL code that looks something like this: select @automation_rate

  • 0

I have a section of T-SQL code that looks something like this:

select @automation_rate = 
  case 
    when @total_count = 0 then 0
    else @automated_count / @total_count
  end

@automation_rate is decimal(3,2). @total_count and @automated_count are integers.

Unfortunately the only values ever returned for @automation_rate are either 0 or 1. Clearly there is something wrong here, and it may be ridiculously simple, but for the life of me I can’t see it.

All data in the underlying resultset (I’m looping over a table) is either 0 or a positive integer. All values are integers except for the automation rate.

Here’s example values and expected (using a calculator) vs actual results:

automated count     total count     expected ratio     actual ratio
---------------     -----------     --------------     ------------
              0              35                0.0             0.00
             98             258              37.98             0.00
             74             557              13.29             0.00
            140             140               1.00             1.00

As you can see I get a ratio of 0.00 for all values except where automated = total. I also have an Excel spreadsheet that does the same basic calculation and it comes out perfect (i.e. just like the “expected” column) every time.

So where did I go wrong?

(This is on MS SQL Server 2005, on the off chance that has any impact at all)

Edit Thanks to everyone for the answers. I blew the integer rounding part by assuming since it was moving into a decimal data type that it would automatically convert, instead of realizing it would do the calculation, round, and then convert. Everyone had similar answers so upvotes all around.

  • 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-06T23:30:49+00:00Added an answer on June 6, 2026 at 11:30 pm

    This seems to be due to integer math, since @automated_count and @total_count are obviously integers. You need to say:

    1.0*@automated_count / @total_count
    

    Or more explicitly:

    CONVERT(DECIMAL(5,2), @automated_count) / @total_count
    

    Also these will yield 0.3798 etc. so you might want:

    CONVERT(DECIMAL(5,2), 100.0*@automated_count / @total_count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a section of code that can be summarised as follows; void MyFunc()
I have a piece of code that I would like to set as part
I have a section of code that depending on the URL requested, will include
I have several instances where that a section of legacy sql statements is based
I have this SQL Statement: de.SqlStatement = SELECT A.Name, A.Catagory , COUNT(Patient_ID) AS PNO
I have a section of code which should be executed by a maximum number
I have a section on my website that when a user clicks I would
I have a section of a website that has three divs next to each
I have a section of my application that allows for people to send an
On my page I have a section where I would like to pull in

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.