Solved

SQL Division

Posted on 2010-09-22
7
469 Views
Last Modified: 2012-05-10
i'm using the following code to divide two number but i want my result to be instead of a whole number but a decimal.  167 / 25 = 6.68.  when i use the following it gives me only 6.

convert(decimal,case when DaysWorked = 0 then 0 else ((TotalHours/60/60) / DaysWorked  end,2) as AverageLaborHours

TotalHours and DaysWorked are both integers.  
TotalHours is in seconds.  that is why i'm dividing it by 60 twice to get the 167.

Please help
0
Comment
Question by:thecruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Assisted Solution

by:mquiroz
mquiroz earned 167 total points
ID: 33736521
you need somenthing like this:

convert(decimal(4, 2),case when DaysWorked = 0 then 0 else ((TotalHours/60/60) / DaysWorked  end,2) as AverageLaborHours

0
 
LVL 3

Assisted Solution

by:_bmendoza
_bmendoza earned 167 total points
ID: 33736672
select intcol1 / (intcol2 * 1.0)
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 166 total points
ID: 33736933
To go along with the last Experts post which shows a valid work around is that MS SQL does INTEGER division when both operands (the numerator and denominator) are INTEGERS; therefore, you have to cast/convert explicitly or implicitly as is the case with multiplying by 1.0 to get one of the operands to be a different data type.

If this is your original query:
convert(decimal,case when DaysWorked = 0 then 0 else ((TotalHours/60/60) / DaysWorked  end,2) as AverageLaborHours

You can simply do something like this:
convert(decimal,case when DaysWorked = 0 then 0 else ((TotalHours/60.0/60.0) / DaysWorked  end,2) as AverageLaborHours

Additional notes:
- to increase efficiency do simple math calculations out ahead of time -- so x / 60.0 / 60.0 is probably best done as x / 3600.0
- convert() syntax is convert({datatype}, {value}) -- decimal is declared as decimal(m, n) where m is total number of digits and n is the number of decimal places; therefore, if I understand you, you are looking for 2 decimal places -- this is incorrect currently and should be convert(decimal(12, 2), {case statement}) where you would replace 12 with different value if needed.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Closing Comment

by:thecruz
ID: 33737308
this did it
0
 
LVL 3

Expert Comment

by:_bmendoza
ID: 33737316
mwvisa1:- so to clarify is
                 select intcol1 / (intcol2 * 1.0)
will not work in ms sql server?
Zones:
MS SQL Server, SQL Server 2005, SQL Server 2008
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33737494
@_bmendoza:
"To go along with the last Experts post which shows a valid work around"

This statement was referring to your post.  I was saying to the thecruz that you post did indeed show a valid work around for the issue which I then explained along with the other corrections.  I was supporting your post.
0
 
LVL 3

Expert Comment

by:_bmendoza
ID: 33737540
Thanks=)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

734 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