Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# SQL Division

Posted on 2010-09-22
Medium Priority
472 Views
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.

0
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

LVL 7

Assisted Solution

mquiroz earned 668 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

_bmendoza earned 668 total points
ID: 33736672
select intcol1 / (intcol2 * 1.0)
0

LVL 60

Accepted Solution

Kevin Cross earned 664 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

- 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

Author Closing Comment

ID: 33737308
this did it
0

LVL 3

Expert Comment

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

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

ID: 33737540
Thanks=)
0

## Featured Post

Question has a verified solution.

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

Why is this different from all of the other step by step guides? Â Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff lâ€¦
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
###### Suggested Courses
Course of the Month11 days, 16 hours left to enroll