Discussion:
%SUBST Varchar and RPG
(too old to reply)
Chris
2005-08-31 19:11:52 UTC
Permalink
Few Issues I'm running into w/VARCHAR and RPG.

Trying to %SUBST on a VARCHAR column in a table. IE.
%Subst(VARCHARFLD:1:15). It bombs, then after a little reading I found
out why (stores the length). So, I thought I could convert it to Char
IE, %CHAR(VARCHARFLD), the Compiler says no. So then I tried the
compiler option, CVTOPT(*VARCHAR). %SUBST worked, but when I tried to
update the DB Table, I received a data mapping error.

What I'm looking for is a way to convert the VARCHAR to a CHAR so I
can do SUBST. Am I missing something? V5R3 If it helps.

Tx

Chris
Chris Pando
2005-08-31 20:56:24 UTC
Permalink
Post by Chris
Few Issues I'm running into w/VARCHAR and RPG.
Trying to %SUBST on a VARCHAR column in a table. IE.
%Subst(VARCHARFLD:1:15). It bombs, then after a little reading I found
out why (stores the length). So, I thought I could convert it to Char
IE, %CHAR(VARCHARFLD), the Compiler says no. So then I tried the
compiler option, CVTOPT(*VARCHAR). %SUBST worked, but when I tried to
update the DB Table, I received a data mapping error.
What I'm looking for is a way to convert the VARCHAR to a CHAR so I
can do SUBST. Am I missing something? V5R3 If it helps.
Tx
Chris
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/books/c0925083163.htm#HDRUSEVLF

Look at Figure 84

The first two bytes of the variable length field are the length, the
rest the data. You use a data structure to break the field into
a decimal variable and a character variable, and substring the
character variable. If you are going to change/update the field
in the database, you are responsible for changing the decimal variable
to the appropriate length.

varChar = string;
varDec = %Len(%TrimR(string));


HTH,

Chris

Chris
--
Steve Richter
2005-08-31 21:04:43 UTC
Permalink
Post by Chris
Few Issues I'm running into w/VARCHAR and RPG.
Trying to %SUBST on a VARCHAR column in a table. IE.
%Subst(VARCHARFLD:1:15). It bombs, then after a little reading I found
out why (stores the length).
I think it bombed because the length was less than 15.
Post by Chris
So, I thought I could convert it to Char
IE, %CHAR(VARCHARFLD), the Compiler says no. So then I tried the
compiler option, CVTOPT(*VARCHAR). %SUBST worked, but when I tried to
update the DB Table, I received a data mapping error.
What I'm looking for is a way to convert the VARCHAR to a CHAR so I
can do SUBST. Am I missing something? V5R3 If it helps.
should just be able to copy the varying field into the char field.

-Steve
Brian
2005-08-31 21:06:12 UTC
Permalink
Post by Chris
Few Issues I'm running into w/VARCHAR and RPG.
Trying to %SUBST on a VARCHAR column in a table. IE.
%Subst(VARCHARFLD:1:15). It bombs, then after a little reading I found
out why (stores the length). So, I thought I could convert it to Char
IE, %CHAR(VARCHARFLD), the Compiler says no. So then I tried the
compiler option, CVTOPT(*VARCHAR). %SUBST worked, but when I tried to
update the DB Table, I received a data mapping error.
What I'm looking for is a way to convert the VARCHAR to a CHAR so I
can do SUBST. Am I missing something? V5R3 If it helps.
Tx
Chris
You could append 15 blanks to insure your substring does not reach past
the end of the data:

%subst( varcharfld + ' ' : 1 : 15 )
Chris
2005-09-01 12:12:34 UTC
Permalink
Thanks all, I use the suggestions noted.

BTW, IMHO IBM should mod %CHAR to convert VARCHAR to character and
another bif (or add another format to %CHAR) to convert CHAR to
VARCHAR.

Chris
Ken
2005-09-01 14:54:01 UTC
Permalink
Hi Chris -
Post by Chris
BTW, IMHO IBM should mod %CHAR to convert VARCHAR to character and
another bif (or add another format to %CHAR) to convert CHAR to
VARCHAR.
Why? A straight eval does that.
--
Ken
http://www.ke9nr.net/
Opinions expressed are my own and do not necessarily represent the views of
my employer or anyone in their right mind.
Chris
2005-09-01 18:04:37 UTC
Permalink
Hmm, correct. Maybe I went about the SUBST issue the wrong way.... Oh
well, leave and learn.

Chris
Ken
2005-09-02 04:44:57 UTC
Permalink
Hi Chris -
Post by Chris
Hmm, correct. Maybe I went about the SUBST issue the wrong way.... Oh
well, leave and learn.
If the varying length field *could* contain as much data the constant
start position and length allow for, the compiler cannot issue an
error. It has to be a run-time error if the length requested exceeds
the current length of the data (just as it would be a run-time error
if a variable start position or length attempted to go beyond the end
of a fixed-length field).

I would code something like:

if %len(varying) >= 15
eval fixedlength = %subst(varying:1:15)
else
eval fixedlength = varying
endif

Of course if fixedlength has a length of 15 or less, all you need is a
straight eval:
eval fixedlength = varying
--
Ken
http://www.ke9nr.net/
Opinions expressed are my own and do not necessarily represent the views
of my employer or anyone in their right mind.
Loading...