Click here to Skip to main content
Click here to Skip to main content

Format Number to 2 Decimals in MS SQL without Rounding

, 15 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Format any Number to 2 decimal Number without Rounding

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 Smile | :)

 

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

Share

About the Author

ashishnaik1

Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberJaiprakash M Bankolli24-Oct-09 4:22 
GeneralDoesn't work for numbers without decimals that should be displayed with trailing .00 PinmemberJaffarkKazi19-Oct-06 20:28 
QuestionWhy not use the floor command? PinmemberFrancisco Prata16-Oct-04 5:38 
AnswerRe: Why not use the floor command? PinsussAnonymous16-Oct-04 8:03 
GeneralRe: Why not use the floor command? PinmemberFrancisco Prata16-Oct-04 9:00 
Questionwithout rouding off? PinsussAnonymous15-Oct-04 8:40 

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.

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