Solved

T-SQL Simple Two Decimal Places

Posted on 2014-04-11
4
757 Views
Last Modified: 2014-04-11
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
Comment
Question by:TBSupport
4 Comments
 
LVL 5

Expert Comment

by:mebaby333
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 65

Accepted Solution

by:
Jim Horn earned 500 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))

Open in new window

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.

T-SQL Integer Division
0
 
LVL 32

Expert Comment

by:ste5an
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) );

Open in new window

0
 
LVL 69

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

23 Experts available now in Live!

Get 1:1 Help Now