?
Solved

SQL Query not showing decimals

Posted on 2014-01-17
3
Medium Priority
?
1,632 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 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39789388
>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
ID: 39789421
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
ID: 39789423
Thanks.  Exactly what I needed to know.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

807 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