Solved

SQL Query doing division

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

Select 10 / 42
0
Comment
Question by:vbnetcoder
[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
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Better way to filter date  - Query 5 44
Please explain Equi-join 3 42
T-SQL: need to reset a declared variable 4 28
Set a variable value in SQL Procedure 3 25
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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