Click here to Skip to main content
15,867,453 members
Articles / Database Development / SQL Server
Article

Format Number to 2 Decimals in MS SQL without Rounding

Rate me:
Please Sign up or sign in to vote.
1.03/5 (17 votes)
15 Oct 2004 146.2K   19   6
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 :)

 

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


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Jaiprakash M Bankolli24-Oct-09 3:22
Jaiprakash M Bankolli24-Oct-09 3:22 
GeneralDoesn't work for numbers without decimals that should be displayed with trailing .00 Pin
JaffarkKazi19-Oct-06 19:28
JaffarkKazi19-Oct-06 19:28 
QuestionWhy not use the floor command? Pin
Francisco Prata16-Oct-04 4:38
Francisco Prata16-Oct-04 4:38 
AnswerRe: Why not use the floor command? Pin
Anonymous16-Oct-04 7:03
Anonymous16-Oct-04 7:03 
GeneralRe: Why not use the floor command? Pin
Francisco Prata16-Oct-04 8:00
Francisco Prata16-Oct-04 8:00 
Questionwithout rouding off? Pin
Anonymous15-Oct-04 7:40
Anonymous15-Oct-04 7:40 

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

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