Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# T-SQL Simple Two Decimal Places

Posted on 2014-04-11
Medium Priority
845 Views
Hello:

I can't believe that I can't find a simple example of this on the internet, somewhere.

All I want is the number 64.25--not 64 and certainly not 64.00.

The best that I can come up with is the following, and it is giving me 64:

CAST(UPR30301.MTDHOURS_4/100 AS DECIMAL(18, 2))

How do I take "UPR30301.MTDHOURS_4/100" and have the result be 64.25?  That's all I want.  It should be very simple.

Thanks!

TBSupport
0
Question by:TBSupport

LVL 5

Expert Comment

ID: 39994729
if your in SSRS you should be able to simply right click on the field in the form and then press properties and format .... then choose the format tab and enter Format Code as "0.00"
0

LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 39994747
>How do I take "UPR30301.MTDHOURS_4/100" and have the result be 64.25
``````UPR30301.MTDHOURS_4/CAST(100 AS DECIMAL(18, 2))
``````
What you are experiencing is 'integer division', where T-SQL will interpret the division of two integers as a result that is also an integer, hence 5 / 2 = 2 and not 2.5.

To avoid this, you need to CAST either the numerator or the denominator as a decimal, and not the entire result.

0

LVL 36

Expert Comment

ID: 39994755
The problem is the data type precedence. Seems that you're dividing an integer by an integer. Thus the result is integer. The additional cast cannot restore the missing digits as they are not part of the result integer. See

``````DECLARE @decimal DECIMAL (19,2) = 100;
DECLARE @integer INT = 100;

SELECT	@decimal / 3,
@integer / 3,
@integer / 3.0,
CAST( @integer / 3.0 AS DECIMAL(19, 2) );
``````
0

LVL 70

Expert Comment

ID: 39994908
CAST(UPR30301.MTDHOURS_4/100.0 AS DECIMAL(18, 2))
0

## Featured Post

Question has a verified solution.

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

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
###### Suggested Courses
Course of the Month9 days, 2 hours left to enroll