Solved

SQL Query doing division

Posted on 2014-11-10
2
144 Views
Last Modified: 2014-11-10
When i run this query it returns a 0.  Why?

Select 10 / 42
0
Comment
Question by:vbnetcoder
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40433766
Because SQL Server treats this as 'integer math', meaning an integer divided by an integer should return an integer value.   Sounds kinda hokey I know.

To return the decimal portion of this expression, either change one of the numbers to include decimal places, or cast one of the values to a decimal data type.
SELECT 10 / 42.0
-- or
SELECT 10 / CAST(42 as numeric(19,4))

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 40433792
Thank you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

743 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

9 Experts available now in Live!

Get 1:1 Help Now