Click here to Skip to main content
Licence 
First Posted 15 Oct 2004
Views 114,489
Bookmarked 19 times

Format Number to 2 Decimals in MS SQL without Rounding

By | 15 Oct 2004 | Article
Format any Number to 2 decimal Number without Rounding
 
Part of The SQL Zone sponsored by
See Also

Introduction

If you wish to Format Number to 2 decimal without rounding off, here is the trick

Create Table in sql

CREATE TABLE [Table1] (
 [area] [float] NULL
)

 

Insert Some data

INSERT INTO Table1 ([area]) VALUES(12.693)
INSERT INTO Table1 ([area]) VALUES(1256.12963)
INSERT INTO Table1 ([area]) VALUES(25.998596)
INSERT INTO Table1 ([area]) VALUES(1.963)

 

Now the query

select
cast(cast(area as int) as varchar(10)) + cast(substring(cast(area-cast(area as int) as varchar(10)),2,3) as varchar(4)),
FROM Table1

Logic:

1) Convert number into integer

2) Subtract interger part from actual number 

3) Convert answer from step 2 to varchar and substring it to take 3 digits including decimal

4) Concate (+) step 1 and 2

Happy SQLing :)

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ashishnaik1



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberJaiprakash M Bankolli3:22 24 Oct '09  
GeneralDoesn't work for numbers without decimals that should be displayed with trailing .00 PinmemberJaffarkKazi19:28 19 Oct '06  
QuestionWhy not use the floor command? PinmemberFrancisco Prata4:38 16 Oct '04  
AnswerRe: Why not use the floor command? PinsussAnonymous7:03 16 Oct '04  
GeneralRe: Why not use the floor command? PinmemberFrancisco Prata8:00 16 Oct '04  
Questionwithout rouding off? PinsussAnonymous7:40 15 Oct '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 15 Oct 2004
Article Copyright 2004 by ashishnaik1
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid