Solved

Decimal places only show two

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

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

13 Experts available now in Live!

Get 1:1 Help Now