Submitted by Anonymous (not verified) on Wed, 01/22/2014 - 00:00
Forums

The students are really complaining about the way the quote (') has to be escaped in RPG compared to other languages. I really never thought about it until now.

The point I was making on the escape character for quote was that the students think it is strange that they have to use '''''''''' in the SELECT string to get a quote.

Niels Liisberg

Wed, 01/22/2014 - 00:00

Other languages (like Python) have the "long string notation" with """ so the string continues until a matching """ is found. Other languages (like JavaScript) can flex between " or ' for the string so it makes most sense in the context. RPG however has none of the above – RPG is rather new to string manipulation and still has place for improvement.

There is a way around it though:
I use a constant named q which has one quote and then connect that into the string. Then just "read" ' when you see a q:

Dcl-c q '''' <= Now this is the only ugly quote line in your code
….
sqlStr = 'Select * from products where manuid = '+q+ manuid + q+ ' order by prodkey';

 

Another way around it is to make a procedure that returns quotes around the string:

sqlStr = 'Select * from products where manuid = ' + quotes(manuid) + ' order by prodkey';

 

Where quotes look something like:

Dcl-proc quotes (
   instring varchar(32766)
   returns varchar(32766)
);

   return (q + instring + q);

Dcl-end;

 

In both of the above you have isolated the ugly '''' for one quote into a designated place…
The last way is to implement the " / ' switching – so you can use another chart for the quote and then %xlate it to the quote at runtime:

sqlstr = 'Select * from products where manuid = "SONY" order by prodkey';

sqlStr = %xlate('"' : q : sqlstr);

 

This however is a little error prone. What if you need the " in string – then it also becomes single quotes.

 

Best regards,
Niels Liisberg