Solved

Formatting number fields

Posted on 2002-06-04
8
212 Views
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
or
123,456.8
I want define thousand delimiter and number decimal places.
In datetime fields is good function convert, but for decimal or integer not.

Thanks , Petr
0
Comment
Question by:svab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 143

Expert Comment

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

CHeers
0
 

Author Comment

by:svab
ID: 7053510
select CONVERT ( money, 1234.67, 1 )
=
1234.6700

Where is my format?
0
 
LVL 143

Expert Comment

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

select convert(varchar(30),convert(money, 32234344.9278),1)
-----------------
32,234,344.93

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

CHeers

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
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.
CHeers
0
 

Author Comment

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

Author Comment

by:svab
ID: 7053597
Thank You angellll
by
0
 
LVL 69

Expert Comment

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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

738 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