473,836 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
+ 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(DEC IMAL(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 23439
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(DEC IMAL(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(DECIM AL(0.01, 6, 2))+1)))
||SUBSTR('0',1, INT(1-SIGN(SIGN(ABS(D ECIMAL(0.01, 6, 2))-1)+1)))
||TRANSLATE(LTR IM(TRANSLATE(RT RIM(CHAR(ABS(DE CIMAL(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(DEC IMAL(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(DE CIMAL(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(DEC IMAL(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(DE CIMAL(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(DE CIMAL(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(DE CIMAL(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(DE CIMAL(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(DE CIMAL(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(DE CIMAL(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(10 20.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(DE CIMAL(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,C ASE 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(DE CIMAL(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,C ASE 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(DE CIMAL(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,C ASE 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

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

Similar topics

7
41380
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 nmea checksum for gps data. e.g. everything between $ and * needs to be xor: $GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287,00,05,25,103,44* to get the checksum.
1
17357
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
13089
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 a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
3
6128
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 small problem with converting Dec to hex and displaying the hex, because when i do the conversion between Dec and Hex, i dont know how i can add a \u properly to the result for the compiler to properly recognize it as a unicode string and not a...
5
7695
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 txtGcost_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 48 To 57, 8 Case 46 If InStr(txtGcost.Text, ".") 0 Then
7
6543
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 roman (dec2rom). int rom2dec(char rom) { //Local Variables int a = 0; //counter int dec = 0;//decimal equivalent
8
2764
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 not working. can some one help me please. Thanks #include <iostream> #include <iomanip> using namespace std;
1
3720
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 DECIMAL(AMT_CHEQUE,31,2) END AS AMT_CHEQUE FROM DB2ADMIN.RPT_BA_PADC161
3
19317
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 display 2100 instead of 1900. I know that I need to come up with a way to determine what letter comes before so program can subtract it but at this point am little stuck. Here is my code: package chapt08; import javax.swing.JOptionPane;
0
9811
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9657
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10822
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10577
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7774
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6975
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.