Author |
Message
|
tbt102 |
Posted: Wed Apr 30, 2003 7:24 am Post subject: CAST with padding. |
|
|
Apprentice
Joined: 21 Apr 2003 Posts: 28
|
Hi all,
In a compute node I have a declared iVar as integer and cVar as char. iVar will be populated with a number no larger than 9999.
I need to cast iVar to cVar with leading 0s when the signifeget digits are less than 4.
I want to do something like this when iVar = 999:
SET cVar = SomePaddingFunction(CAST(iVar AS CHAR))
cVar contains 0999
Can someone help with the SomePaddingFunction?
Thanks in advance for any help. |
|
Back to top |
|
 |
RamVijayawada |
Posted: Wed Apr 30, 2003 7:37 am Post subject: |
|
|
Novice
Joined: 22 Apr 2003 Posts: 15
|
First cast iVar to cVar..
Then find length
DECLARE N INTEGER;
DECLARE M INTEGER;
SET N = LENGTH(iVar);
IF N < 4 THEN
SET M = 4 - N;
WHILE M >0 DO
SET iVar = '0' || iVar;
SET M= M-1;
END WHILE;
END IF;
Pull out the iVar, u c the padded iVar....
Hope this helps... |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Apr 30, 2003 9:12 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
You could use the OVERLAY function for this.
Code: |
set cVar = cast(iVar as char);
if (length(cVar) < 4) then
set cVar = overlay('0000' placing cVar from (4 - length(cVar));
end if; |
But you don't say what version of WMQI you're running, and what CSD level, so you may not have the OVERLAY function available. |
|
Back to top |
|
 |
RamVijayawada |
Posted: Wed Apr 30, 2003 10:00 am Post subject: |
|
|
Novice
Joined: 22 Apr 2003 Posts: 15
|
Jeff, if the value is 99 according to u
the result will be 0990--according my understanding...
but he wants 0099--
It should be
set cVar = cast(iVar as char);
if (length(cVar) < 4) then
set cVar = overlay('0000' placing cVar from (5 - length(cVar));
end if; |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Apr 30, 2003 11:47 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Ooops. Off by one error.
Too much C recently, sorry. |
|
Back to top |
|
 |
RamVijayawada |
Posted: Wed Apr 30, 2003 11:54 am Post subject: |
|
|
Novice
Joined: 22 Apr 2003 Posts: 15
|
Cool, overlay is good if its the matter of 4-5 characters. One has to go for a loop it its the case of a real big long String.. |
|
Back to top |
|
 |
kwelch |
Posted: Wed May 07, 2003 12:38 pm Post subject: |
|
|
 Master
Joined: 16 May 2001 Posts: 255
|
Are you using the MRM? If so you can assign a padding character there.
Karen |
|
Back to top |
|
 |
|