Solved

SQL Query not showing decimals

Posted on 2014-01-17
3
1,088 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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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