?
Solved

SQL Query not showing decimals

Posted on 2014-01-17
3
Medium Priority
?
1,444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

765 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