TechTips

Embedded SQL : Easy String Manipulation Using SQL Functions


SQL is always much better than RPG for handling string data.

Embedding SQL in RPG gives RPG the great power not only to handle database operations but also to perform string manipulation. For string manipulation, RPG provides some very basic functions, such as %TRIM, %SUBST, %SCAN, and %REPLACE.

CISSP pdf The geology of the surrounding CISSP pdf country 300-208 questions possesses little interest. CISSP pdf CISSP pdf Throughout the coast of Brazil, 200-105 question and certainly 300-208 questions for a considerable space inland from the CISSP pdf Rio Plata to Cape St. CISSP pdf Roque, lat. 5S., a distance 300-208 questions of 200-105 question more than 2000 geographical miles, wherever solid CISSP pdf 300-208 questions 200-105 question rock occurs, CISSP pdf 200-105 question it belongs 300-208 questions 200-105 question to a granitic formation. The circumstance of this enormous area 200-105 question being thus 200-105 question constituted 200-105 question of materials, which almost 300-208 questions every geologist believes have been crystallized by the action of heat under pressure, 300-208 questions 300-208 questions gives 300-208 questions rise to many curious 300-208 questions reflections. CISSP pdf Was this effect produced beneath the depths of a profound ocean? Or 200-105 question did 200-105 question a covering of strata formerly extend over it, which has since been removed? Can we CISSP pdf believe that any power, action for a time short of CISSP pdf infinity, could CISSP pdf have denuded 300-208 questions CISSP pdf the CISSP pdf 200-105 question granite over 200-105 question so many thousand square leagues?

A small forest in my memorythis 300-208 questions is a forest that not as 200-105 question large as a collage, but it is all of my childhood. in 300-208 questions summer, CISSP pdf i can 300-208 questions smell sweet from different flowers, and play games with CISSP pdf friends behind the trees. i also can hear the sounds of owl and birds. 200-105 question and rabbites and hedgehog. in winter, there is a white world, i can 300-208 questions play snow with my 300-208 questions friends, and make snow man. 200-105 question there are most 200-105 question improtatant memory for me,and i can not forget this period of my life.


Suppose you want to convert a string to uppercase. How can you do that in RPG? By using the %XLATE function and a couple of constants containing all 26 letters of the alphabet. Something like this:

GoyalFig1Here’s the output:

GoyalFig4

Technically this is more of search and replace function where %XLATE search each character of LOWER and replace that with corresponding character of UPPER. Changing the case of a string is a basic concept in “string manipulation.” Most languages, including SQL, offer simple functions for this.

Here’s an example of how we can use the SQL UPPER function to convert a string into uppercase (or LOWER to convert into lowercase).

GoyalFig3

Here’s the output:

GoyalFig2

EXEC SQL tells the system that this is an SQL statement. SET is used in SQL to set/update the value of the variables. In this example, we passed a host variable NAME to SQL function UPPER and assigned the value back to the same host variable using SET. We can use the SQL LOWER function the same way to convert to lowercase.

Let’s Look at SCAN and REPLACE

I have a string ‘this is a fine example to explain fine SQL’. Now I want to replace all occurrences of fine with good. In RPG, I have to use a loop, inside which I have to use two functions:

  1. %SCAN to find fine
  2. %REPLACE to replace fine with good

The RPG code will be as shown below. First, find the position of fine using %SCAN, and then try to replace it with good based on position, as %REPLACE works only with position, not the actual word.

GoyalFig5

Output:

GoyalFig6

We can achieve the same result with the SQL REPLACE function. In this example, three parameters are passed to the SQL REPLACE function:

  1. String: This is the host variable containing the source string.
  2. ‘fine’ : This is the string we want to find in the source string. We can use the host variable also.
  3. ‘good’: This is new string we want in the source. We can use the host variable also.

The REPLACE function will find all the occurrences for fine and replace it with good.

GoyalFig7

Here’s the output:

GoyalFig8

Example Use of %TRIM

RPG’s %TRIM function removes leading or trailing blanks. What if I have a string like ‘this is a very good website@@@’ and I want to remove all trailing @ characters. With the SQL TRIM function, I can achieve this very easily. Here’s an example:

GoyalFig9

Here’s the output:

GoyalFig10

In the same way, we can use any SQL function in RPG. Check this link for a list of available SQL functions.

 


IBM i developer.

View Comments