473,471 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DECIMAL into CHAR

Hi!

Let's have a data of type DECIMAL(6, 2) and with value 0.01.
How can I convert this one into a "pretty" CHAR?
I've tried
VALUES(CHAR(DECIMAL(0.01, 6, 2)))
and I get
0000.01
which is not nice.

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Dec 16 '06 #1
13 23181
Gregor Kovac( wrote:
Hi!

Let's have a data of type DECIMAL(6, 2) and with value 0.01.
How can I convert this one into a "pretty" CHAR?
I've tried
VALUES(CHAR(DECIMAL(0.01, 6, 2)))
and I get
0000.01
which is not nice.
STRIP/TRIM will do what you need in DB2 9

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 16 '06 #2
I assumed DECIMAL presision and scale are arbitrary chosen.
I can't imagine simpler than this. I feel that this way is too
strightforwards.
I hope someone show more elegant way.
------------------------------ Commands Entered
------------------------------
VALUES SUBSTR('-',1,INT(1-SIGN(SIGN(DECIMAL(0.01, 6, 2))+1)))
||SUBSTR('0',1,INT(1-SIGN(SIGN(ABS(DECIMAL(0.01, 6, 2))-1)+1)))
||TRANSLATE(LTRIM(TRANSLATE(RTRIM(CHAR(ABS(DECIMAL (0.01, 6,
2)))),'','0')),'0',' ');
------------------------------------------------------------------------------

1
----------
0.01

Dec 16 '06 #3
If you simply used STRIP, '0' before decimal point('.') will be lost.
And if value is negative, I guess it is not so simple to add leading
minus sign('-').
(Perhaps, CASE expression, etc will be necessary)

------------------------------ Commands Entered
------------------------------
VALUES STRIP(CHAR(ABS(DECIMAL(0.01, 6, 2))),L,'0');
------------------------------------------------------------------------------

1
-----------
..01

1 record(s) selected.

Dec 16 '06 #4

Gregor Kovac wrote:
Hi!

Let's have a data of type DECIMAL(6, 2) and with value 0.01.
How can I convert this one into a "pretty" CHAR?
I've tried
VALUES(CHAR(DECIMAL(0.01, 6, 2)))
and I get
0000.01
which is not nice.
IMO formatting results should not be done in the database layer.
Anyhow, heres one attempt removing leading zeroes using a cte. You can
wrap it in a scalar function

[ltjn@lelles ~]$ db2 "with t (s,n) as (VALUES(CHAR(DECIMAL(0.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,2,1) =
'0' and n<100) select s from t where n = (select max(n) from t)"

S
--------
0.01

1 record(s) selected.
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Agreed :-)

/Lennart

Dec 16 '06 #5

Lennart wrote:
Gregor Kovac wrote:
Hi!

Let's have a data of type DECIMAL(6, 2) and with value 0.01.
How can I convert this one into a "pretty" CHAR?
I've tried
VALUES(CHAR(DECIMAL(0.01, 6, 2)))
and I get
0000.01
which is not nice.

IMO formatting results should not be done in the database layer.
Anyhow, heres one attempt removing leading zeroes using a cte. You can
wrap it in a scalar function

[ltjn@lelles ~]$ db2 "with t (s,n) as (VALUES(CHAR(DECIMAL(0.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,2,1) =
'0' and n<100) select s from t where n = (select max(n) from t)"

S
--------
0.01

1 record(s) selected.
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

Agreed :-)

/Lennart
If initial value is more than 1, extra 0 will be remained.
------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(1.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,2,1) =
'0' and n<100) select s from t where n = (select max(n) from t);
------------------------------------------------------------------------------

S
--------
01.01

And if initial value is 1020.01(more than 1000 and second digit is '0',
result will be wrong.
------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(1000.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,2,1) =
'0' and n<100) select s from t where n = (select max(n) from t);
------------------------------------------------------------------------------

S
--------
020.01
To correct these two case, one idea is to modify following.
------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(0.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,1,1) =
'0' and n<100)
select SUBSTR(s,CASE WHEN SUBSTR(s,2,1) = '.' OR n = 0 THEN 1 ELSE 2
END) S
from t where n = (select COALESCE(NULLIF(max(n)-1,-1),0) from t)
;
------------------------------------------------------------------------------

S
--------
0.01

------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(1.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,1,1) =
'0' and n<100)
select SUBSTR(s,CASE WHEN SUBSTR(s,2,1) = '.' OR n = 0 THEN 1 ELSE 2
END) S
from t where n = (select COALESCE(NULLIF(max(n)-1,-1),0) from t)
;
------------------------------------------------------------------------------

S
--------
1.01

------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(1020.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,1,1) =
'0' and n<100)
select SUBSTR(s,CASE WHEN SUBSTR(s,2,1) = '.' OR n = 0 THEN 1 ELSE 2
END) S
from t where n = (select COALESCE(NULLIF(max(n)-1,-1),0) from t)
;
------------------------------------------------------------------------------

S
--------
1020.01

Dec 17 '06 #6

Tonkuma wrote:
[...]
If initial value is more than 1, extra 0 will be remained.
[...]

Yes, you are right. At first sight I thought adding a dummy '0' at the
beginning of the initial string
would help

db2 "with t (s,n) as (VALUES('0' || CHAR(DECIMAL(1020.01, 6, 2)),0)
union all select substr(s,2),n+1 from t where substr(s,1,1) = '0' and
n<100) select s from t where n = (select max(n) from t)"

S
---------
1020.01

but we still need to handle the case with one leading '0'.

/Lennart

Dec 17 '06 #7
This will be more simple than my previous post..
------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(0.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,1,1) =
'0' and n<100)
select SUBSTR('0'||s,CASE WHEN SUBSTR(s,1,1)='.' THEN 1 ELSE 2 END) S
from t where n = (select max(n) from t)
;
------------------------------------------------------------------------------

S
---------
0.01

Dec 17 '06 #8
oh, there're negative numbers:)

"Tonkuma д
"
This will be more simple than my previous post..
------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(0.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,1,1) =
'0' and n<100)
select SUBSTR('0'||s,CASE WHEN SUBSTR(s,1,1)='.' THEN 1 ELSE 2 END) S
from t where n = (select max(n) from t)
;
------------------------------------------------------------------------------

S
---------
0.01
Dec 18 '06 #9
I mean for a general ....

"Hardy д
"
oh, there're negative numbers:)

"Tonkuma д
"
This will be more simple than my previous post..
------------------------------ Commands Entered
------------------------------
with t (s,n) as (VALUES(CHAR(DECIMAL(0.01, 6,
2)),0) union all select substr(s,2),n+1 from t where substr(s,1,1) =
'0' and n<100)
select SUBSTR('0'||s,CASE WHEN SUBSTR(s,1,1)='.' THEN 1 ELSE 2 END) S
from t where n = (select max(n) from t)
;
------------------------------------------------------------------------------

S
---------
0.01
Dec 18 '06 #10
db2 =select * from decimal;

DEC
--------
1020.01
0.01
-0.01

3 条记录已选择。

db2 =with t(an,mark, lzero) as ( select strip(char(abs(dec)),l,'0'),
case when
dec < 0 then '-' else '' end case , case when abs(dec) < 1 then '0'
else '' end
case from decimal)
db2 (cont.) =select mark||lzero||an from t;

1
----------
1020.01
0.01
-0.01

3 条记录已选择。

"Gregor Kovač 写道:
"
Hi!

Let's have a data of type DECIMAL(6, 2) and with value 0.01.
How can I convert this one into a "pretty" CHAR?
I've tried
VALUES(CHAR(DECIMAL(0.01, 6, 2)))
and I get
0000.01
which is not nice.

Best regards,
Kovi
--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Dec 18 '06 #11
Ian
Lennart wrote:
IMO formatting results should not be done in the database layer.
Amen!

Why is it that developers always claim that the database HAS to return
formatted values?

Dec 18 '06 #12

Ian wrote:
Lennart wrote:
[...]
Why is it that developers always claim that the database HAS to return
formatted values?
They probably don't have the time to format the data, since they are to
busy writing business rules, that should have been declared in the
database :-)

/Lennart

Dec 18 '06 #13
Ian wrote:
Lennart wrote:
>IMO formatting results should not be done in the database layer.

Amen!

Why is it that developers always claim that the database HAS to return
formatted values?
Well, it wasn't the developers who made the decision, but the head of the
software group, so to speak. Don't ask me why, he just loves to have
everything in the database.

--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Dec 19 '06 #14

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

Similar topics

7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
1
by: Jean-michel | last post by:
I need to convert a decimal field to char() but also trim the leading zeroes. Any idea? I could not find any function to do that.
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
3
by: Andres A. | last post by:
I have bunch of unicode characters stored as Decimal is there a easy way of displaying unicode from Decimal numbers or do i have to convert the decimal to hex then display the hex? i ran into a...
5
by: trawlerman | last post by:
Hi all, C# newbie after migrating from VB. I wish to have only valid numbers entered into a txtbox . i.e only one decimal point. ///////////////////My VB code for this is: Private Sub...
7
by: billbaitsg | last post by:
Hi, I need some help with figuring out why my program is all messed up. There are two portions two it, one that converts from roman to decimal (rom2dec) and another that converts from decimal to...
8
by: sdlt85 | last post by:
Hi, the program is asking the user for a number ant he base of the number, then it will convert the number to decimal. But is running right only with base 2, 3, 4, 5, 6, 7, 8 but if I try 16 it is...
1
by: karanbikash | last post by:
Hi , I have a requirement where in I need to display the decimal value of 0.00 as '######.00' SELECT case when DECIMAL(AMT_CHEQUE,31,2) = 0.00 THEN 9999999999999.9999999 ELSE ...
3
by: capoeira26 | last post by:
I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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 projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.