Solved

SQL Division

Posted on 2010-09-22
7
464 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
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 59

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 59

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

948 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

22 Experts available now in Live!

Get 1:1 Help Now