473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trailing 0's

I have a column defined as DECIMAL(10,6). I want to display it as a
string but I do not want the trailing zeros. I cannot seem to get CAST
or CONVERT or STR to exclude the zeros.

Examples:
45.340000 --> 45.34
27.700000 --> 27.7
55.000000 --> 55

Is there a function that will do this or do I need to write my own?
Jul 20 '05 #1
5 15343

"Jason" <Ja*******@hotmail.com> wrote in message
news:f0**************************@posting.google.c om...
I have a column defined as DECIMAL(10,6). I want to display it as a
string but I do not want the trailing zeros. I cannot seem to get CAST
or CONVERT or STR to exclude the zeros.

Examples:
45.340000 --> 45.34
27.700000 --> 27.7
55.000000 --> 55

Is there a function that will do this or do I need to write my own?


It's usually best to do this formatting in the client, rather than the
database layer - client tools will have much better formatting options, and
they allow for changing the formatting dynamically without having to
resubmit the query. But here's one way to do it:

select case
when right(replace(convert(varchar, MyColumn), '0', ''), 1) = '.'
then replace(replace(convert(varchar, MyColumn), '0', ''), '.', '')
else replace(convert(varchar, MyColumn), '0', '')
end
from dbo.MyTable

Simon
Jul 20 '05 #2
Hi Jason,

I'm not sure if you can find a function in TSQL that does what you
want in this case. But you can use a bunch of string functions to do
the trick, like this:
select rtrim(
replace(
replace(
rtrim(
replace(
cast(DecField as char(11)),
'0',
' '
)
),
' ',
'0'
) + ' ',
'. ',
''
)
)
from YourTable

Let me explain what it does.
Assume you have these decimal values: 43.035000 and 55.000000

1. Convert decimal value into string
x = cast(DecField as char(11))
'43.035000 '
'55.000000 '

2. Replace 0's with space
x = replace(x, '0', ' ')
'43. 35 '
'55. '

3. Trim the trailing spaces
x = RTrim(x)
'43. 35'
'55.'

4. Replace spaces with 0
x = replace(x, ' ', '0')
'43.035'
'55.'

As you see by now all noninteger values are ready, but still we need
to get rid of decimal point appearing at the end of integer values.

5. Add a single space to the end
x = x + ' '
'43.035 '
'55. '

6. Replace '. ' with an empty string
x = replace(x, '. ', '')
'43.035 '
'55'

7. Finally get rid of remaining spaces
x = RTrim(x)
'43.035'
'55'
Well, this works fine but it's not the most efficient to do it.
Depending on size of your table and the number of decimal fields you
want to show, it might take a while. I personally prefer to perform
such cosmetic transformations in front-end application at the time you
display the values. Of course it depends on your programming
environment and its features.

Good luck,
Shervin

Ja*******@hotmail.com (Jason) wrote in message news:<f0**************************@posting.google. com>...
I have a column defined as DECIMAL(10,6). I want to display it as a
string but I do not want the trailing zeros. I cannot seem to get CAST
or CONVERT or STR to exclude the zeros.

Examples:
45.340000 --> 45.34
27.700000 --> 27.7
55.000000 --> 55

Is there a function that will do this or do I need to write my own?

Jul 20 '05 #3
Hi Simon,

This script removes all the 0's, not just trailing ones.

Shervin

"Simon Hayes" <sq*@hayes.ch> wrote in message news:<3f**********@news.bluewin.ch>...
"Jason" <Ja*******@hotmail.com> wrote in message
news:f0**************************@posting.google.c om...
I have a column defined as DECIMAL(10,6). I want to display it as a
string but I do not want the trailing zeros. I cannot seem to get CAST
or CONVERT or STR to exclude the zeros.

Examples:
45.340000 --> 45.34
27.700000 --> 27.7
55.000000 --> 55

Is there a function that will do this or do I need to write my own?


It's usually best to do this formatting in the client, rather than the
database layer - client tools will have much better formatting options, and
they allow for changing the formatting dynamically without having to
resubmit the query. But here's one way to do it:

select case
when right(replace(convert(varchar, MyColumn), '0', ''), 1) = '.'
then replace(replace(convert(varchar, MyColumn), '0', ''), '.', '')
else replace(convert(varchar, MyColumn), '0', '')
end
from dbo.MyTable

Simon

Jul 20 '05 #4
Thanks for the help on this matter.

I agree with both of you. The formatting SHOULD be done on the client
when possible but in this case I have no choice. The logic will be
used in a function on the server when combining several fields to make
a new one.

Sh**********@hotmail.com (Shervin Shapourian) wrote in message news:<4e**************************@posting.google. com>...
Hi Jason,

I'm not sure if you can find a function in TSQL that does what you
want in this case. But you can use a bunch of string functions to do
the trick, like this:
select rtrim(
replace(
replace(
rtrim(
replace(
cast(DecField as char(11)),
'0',
' '
)
),
' ',
'0'
) + ' ',
'. ',
''
)
)
from YourTable

Let me explain what it does.
Assume you have these decimal values: 43.035000 and 55.000000

1. Convert decimal value into string
x = cast(DecField as char(11))
'43.035000 '
'55.000000 '

2. Replace 0's with space
x = replace(x, '0', ' ')
'43. 35 '
'55. '

3. Trim the trailing spaces
x = RTrim(x)
'43. 35'
'55.'

4. Replace spaces with 0
x = replace(x, ' ', '0')
'43.035'
'55.'

As you see by now all noninteger values are ready, but still we need
to get rid of decimal point appearing at the end of integer values.

5. Add a single space to the end
x = x + ' '
'43.035 '
'55. '

6. Replace '. ' with an empty string
x = replace(x, '. ', '')
'43.035 '
'55'

7. Finally get rid of remaining spaces
x = RTrim(x)
'43.035'
'55'
Well, this works fine but it's not the most efficient to do it.
Depending on size of your table and the number of decimal fields you
want to show, it might take a while. I personally prefer to perform
such cosmetic transformations in front-end application at the time you
display the values. Of course it depends on your programming
environment and its features.

Good luck,
Shervin

Jul 20 '05 #5
create table #t (c decimal(10,6))
insert into #t values (45.34)
insert into #t values (27.7)
insert into #t values (55)
insert into #t values (101.1)

select c,
replace(rtrim(replace(
replace(rtrim(replace(
cast(c as varchar(12))
,'0',' ')),' ','0')
,'.',' ')),' ','.')
from #t

drop table #t

Gert-Jan

Jason wrote:

I have a column defined as DECIMAL(10,6). I want to display it as a
string but I do not want the trailing zeros. I cannot seem to get CAST
or CONVERT or STR to exclude the zeros.

Examples:
45.340000 --> 45.34
27.700000 --> 27.7
55.000000 --> 55

Is there a function that will do this or do I need to write my own?

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
23
by: Hostile17 | last post by:
I keep coming across people, online and in real life, who believe that to code single tags like <br> and <img> with trailing slashes, <br /> and <img /> is considered "best practice" and when...
5
by: Stan Shankman | last post by:
How do I add trailing spaces within a right-justified textBox? In order to add a trailing space to the text in a right-justified textBox, I use to just append a space to the end of the text...
3
by: V. Jenks | last post by:
I need to store money values in C# and I noticed when using a float, the trailing zero was trimmed off automatically. I don't want this, is there a native type that will *not* do this so I can...
0
by: Seth | last post by:
First off, my apologies if this is in the wrong newsgroup, but I hope I'm close enough. I'm trying to do some parsing of a CSV file using OleDbConnection, but for some reason, when I populate my...
4
by: Albert | last post by:
This isn't entirely related to C, but Kernighan and Ritchie asks in Execise 1-18 of their C programming language book to 'Write a program to remove trailing blanks and tabs from each line of input,...
17
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can...
9
by: Chester | last post by:
I'm working on an app that records data collected by service technicians (VB.Net front-end, SQL Server 2000 back end). The technicians need to record numbers with varying scale and precision. For...
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
5
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.