Formatting number fields

Posted on 2002-06-04
Last Modified: 2008-02-01
Hi, how can I format number field in TSQL ? For example, in database I have field decimal(9,3) with value 123456.789 and I want get
123 456.79
I want define thousand delimiter and number decimal places.
In datetime fields is good function convert, but for decimal or integer not.

Thanks , Petr
Question by:svab
  • 4
  • 3
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7053496
convert to money:
select CONVERT ( money, yourfield, 1 )


Author Comment

ID: 7053510
select CONVERT ( money, 1234.67, 1 )

Where is my format?
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7053531
sorry, i forgot the second convert:

select convert(varchar(30),convert(money, 32234344.9278),1)

The convert to varchar needs to be from money to use the style property. See the convert topic in the BOL



Author Comment

ID: 7053560
May I change number decimal places ? I want have 0,1 and 2.
And thousend delimiter is also fix, ",".
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 100 total points
ID: 7053579
unfortunately, this is not possible.
Of course, you could REPLACE the "," by " ", and eventually truncate the decimal places using

v is the formatted column like above,d the number of decimale:
for d=0:  LEFT( v , CHARINDEX ( '.', v ))
for d=1:  LEFT( v , CHARINDEX ( '.', v )+2)
for d=2:  LEFT( v , CHARINDEX ( '.', v )+3)
d>2 not possible :-)

Now, if you really want better control of formatting, I would rather use the front-end language which typically have much better options to do so. Just an idea.

Author Comment

ID: 7053594
May I change number decimal places ? I want have 0,1 and 2.
And thousend delimiter is also fix, ",".

Author Comment

ID: 7053597
Thank You angellll
LVL 69

Expert Comment

ID: 7054944
If you are in SQL 2000, you could write a function which would give you any specific format you wanted.

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

12 Experts available now in Live!

Get 1:1 Help Now