niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
to_char number format with optional decimal-point?
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Martin T.
*nix forums beginner


Joined: 07 Mar 2005
Posts: 39

PostPosted: Thu Jul 20, 2006 10:53 am    Post subject: to_char number format with optional decimal-point? Reply with quote

Hello all!

Basically, what I want is the following.
Num -> Str
1 -> '1'
10 -> '10'
1.01 -> '1.01'

Is there a possible Number Format Model that will not display the
decimal if there are no decimal digits?
(I know this is the default behaviour of to_char, but I need aditional
formatting, so I need to spec the format model.)

Related to this: Is it possible to specify a number-format model that
will exactly reproduce the to_char default behaviour?

thanks!

best,
Martin
Back to top
G Quesnel
*nix forums addict


Joined: 29 Apr 2005
Posts: 77

PostPosted: Thu Jul 20, 2006 11:51 am    Post subject: Re: to_char number format with optional decimal-point? Reply with quote

I don't have the answer to your question, but if you can not find the
formating string then you could produce the desired result by adding a
case structure around your to_char formatting. Something like ...
SQL> Select (case when 10.01 > trunc(10.01)
2 then trim(trailing '0' from to_char(10.01,'990.999'))
3 else to_char(10.01,'999') end) A_Number
4 from dual;

A_NUMBER
--------
10.01

or perhaps you could wrap the formatted column in two trim statement:
- first to remove trailing '0'
- second, to remove trailing '.'

Hopefully, someone else can come up with a formating string.
Back to top
Martin T.
*nix forums beginner


Joined: 07 Mar 2005
Posts: 39

PostPosted: Fri Jul 21, 2006 7:16 am    Post subject: Re: to_char number format with optional decimal-point? Reply with quote

G Quesnel wrote:
Quote:
I don't have the answer to your question, but if you can not find the
formating string then you could produce the desired result by adding a
case structure around your to_char formatting. Something like ...
snip
Hopefully, someone else can come up with a formating string.

Thanks for your tip! Seems a pretty clean solution.

Let me state something that may spark some more response Wink ...

1.) It is *not* possible with the ORACLE to_char function (in version
9i2) to specify a number format model that will reproduce the default
behaviour of the function w/o format model.
2.) It is, specifically, *not* possible to describe a number format
model such that the decimal-point is not displayed if there are no
decimals and is displayed if there are significant decimals.

Now - I do also *not* claim that this is the absolute truth, but it
(sadly) will remain my truth until someone proves me wrong.

best,
Martin

p.s.: I guess in 10g there would be some really nice solution with a
regexp Smile
Back to top
skuzworks@googlemail.com
*nix forums beginner


Joined: 21 Jul 2006
Posts: 1

PostPosted: Fri Jul 21, 2006 9:23 am    Post subject: Re: to_char number format with optional decimal-point? Reply with quote

Martin T. wrote:
Quote:
G Quesnel wrote:
I don't have the answer to your question, but if you can not find the
formating string then you could produce the desired result by adding a
case structure around your to_char formatting. Something like ...
snip
Hopefully, someone else can come up with a formating string.

Thanks for your tip! Seems a pretty clean solution.

Let me state something that may spark some more response Wink ...

1.) It is *not* possible with the ORACLE to_char function (in version
9i2) to specify a number format model that will reproduce the default
behaviour of the function w/o format model.
2.) It is, specifically, *not* possible to describe a number format
model such that the decimal-point is not displayed if there are no
decimals and is displayed if there are significant decimals.

Now - I do also *not* claim that this is the absolute truth, but it
(sadly) will remain my truth until someone proves me wrong.

best,
Martin

p.s.: I guess in 10g there would be some really nice solution with a
regexp Smile

Hello,

Just a thought: does CAST solve this problem?

SQL> select cast (1 as varchar(30)) from dual;

CAST(1ASVARCHAR(30))
------------------------------
1

SQL> select cast (100 as varchar2(30)) from dual;

CAST(100ASVARCHAR2(30))
------------------------------
100

SQL> select cast (1.1001 as varchar2(30)) from dual;

CAST(1.1001ASVARCHAR2(30))
------------------------------
1.1001

SQL> select cast (.100 as varchar2(30)) from dual;

CAST(.100ASVARCHAR2(30))
------------------------------
..1

SQL> select cast (1.100 as varchar2(30)) from dual;

CAST(1.100ASVARCHAR2(30))
------------------------------
1.1

SQL> select cast (1.000 as varchar2(30)) from dual;

CAST(1.000ASVARCHAR2(30))
------------------------------
1
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Wed Dec 03, 2008 8:40 pm | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts mailq output format bacilko1 Postfix 0 Tue Oct 03, 2006 12:34 pm
No new posts Does stream I/O support "%a" floating-point format? John Friedland C++ 3 Fri Jul 21, 2006 9:26 am
No new posts number of words in a line Fred J. C++ 3 Fri Jul 21, 2006 3:52 am
No new posts determine pointer to point to array or single item during... yancheng.cheok@gmail.com C++ 5 Fri Jul 21, 2006 1:17 am
No new posts Finding a point inside a polygon Mark Maunder MySQL 2 Thu Jul 20, 2006 11:10 pm

0 Credit Cards | Php Scripts | Property in Spain | Loans | The eBay Song
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1410s ][ Queries: 16 (0.0483s) ][ GZIP on - Debug on ]