Solved

Decimal places only show two

Posted on 2014-11-11
4
109 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 13

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

831 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