Solved

Decimal places only show two

Posted on 2014-11-11
4
100 Views
Last Modified: 2014-11-11
Select Number From Table

is returning

12.4653453544

i would like only the first two number after the decimal so it should be

12.46

HOW?
0
Comment
Question by:vbnetcoder
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40435074
Rounding or no rounding?

-- Rounding
Select CAST(Number as numeric(9,2)) From Table

-- No rounding (probably a better way to pull this off)
SELECT CAST(FLOOR(number * 100) / 100 as decimal(9,2)) From Table
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
ID: 40435123
Jim, just curious, why do you cast the field rather than just using ROUND or FLOOR directly?
0
 

Author Closing Comment

by:vbnetcoder
ID: 40435134
ty
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40435142
>why do you cast the field rather than just using ROUND or FLOOR directly?
Round does the rounding, but doesn't change the data type, so rounding to two decimal places returns 12.4700000.  A fun nugget of info I learned the last time I sat for the 70-433 SQL 2008 Development exam.
Declare @number decimal(19,9) = 12.4653453544
Select CAST(@number as numeric(9,2))
SELECT ROUND(@number, 2) 

Open in new window


Thanks for the grade.  Good luck with your project.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

910 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

23 Experts available now in Live!

Get 1:1 Help Now