Solved

SQL Query not showing decimals

Posted on 2014-01-17
3
1,003 Views
Last Modified: 2014-01-17
I have the SQL Query:
CAST((6000/1400) AS DECIMAL(10,7))
I want the result to be 4.2857143, but instead the result is 4.0000000.  It's displaying the whole number but no decimal numbers.  I'm still a SQL novice, so I'm guessing I missed something.
0
Comment
Question by:David_XF
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
>CAST((6000/1400) AS DECIMAL(10,7))

One of the 'features' of SQL Server is that if two integers participate in a divide operation, it assumes that the result should be an integer.  

To prevent this from happening, either the numerator or demonator, and not the result, needs to be cast as a numeric value that supports decimal places.
SELECT 6000/CAST(1400 AS DECIMAL(19,4)) AS RESULT
SELECT CAST(6000 AS DECIMAL(19,4))/1400 AS result

Open in new window

0
 

Expert Comment

by:Mike_Yashimski
Comment Utility
Depending on the needs of the query you can also simply add .0 at the end of one of your integer values. This accomplishes the same as above. I would recommend only using this for quick ad hoc queries. Use the above method for any queries that will be used more than once.

CAST((6000.0/1400) AS DECIMAL(10,7))
0
 

Author Comment

by:David_XF
Comment Utility
Thanks.  Exactly what I needed to know.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

7 Experts available now in Live!

Get 1:1 Help Now