CHANGING TWO LETTERS 1/3
Posted in 1995
J> From: john@miso.wwa.com (pinoy_ako)
J> We have to change two letters in the gear field specifically the
> letters "SB" to "SD". The problem is that the position of the
> letters are different. These are some of the possible formats:
...
J> As you can see, the "SB" is at different positions. So is there a
> way to do this using SQL? (dbaccess) Pls. post or email. Thanks!
Here's a way in 4gl:
LET returned_string = replace(source_string, old_pattern, new_pattern)
#########################################################################
# #
# This function replaces all occurrences of the 'Old_Pattern' in the #
# 'Source_String' with the 'New_Pattern'. After all replacements are #
# completed, this function returns the modified string. #
# #
#########################################################################
FUNCTION replace(source_string, old_pattern, new_pattern)
#########################################################################
# #
# NOTE: Adjust old and new pattern character lengths to suit your #
# application. The source string 'Source_String' is at INFORMIX's #
# maximum length for passing as a parameter. #
# #
#########################################################################
DEFINE
#########################################################################
# #
# Source_String source string that needs replacement done #
# old_pattern the string to be found in the source #
# new_pattern the string to replace the old_pattern #
# i index pointer to each character in the string #
# old_len length of the old_pattern #
# new_len length of the new_pattern #
# str_len length of the original source string #
# #
#########################################################################
source_string CHAR (512),
old_pattern CHAR (20),
new_pattern CHAR (20),
i SMALLINT,
old_len SMALLINT,
new_len SMALLINT,
str_len SMALLINT
#########################################################################
# #
# Get the lengths of the parameters passed in #
# #
#########################################################################
LET old_len = LENGTH(old_pattern CLIPPED)
LET new_len = LENGTH(new_pattern CLIPPED)
LET str_len = LENGTH(source_string CLIPPED)
IF old_len = 0 OR str_len = 0 THEN
#########################################################################
# #
# Got a null string, return without any changes being made #
# #
#########################################################################
RETURN source_string
END IF
#########################################################################
# #
# Set the index pointer into the string, 0 = before first character #
# #
#########################################################################
LET i = 0
#########################################################################
# #
# Go through each character of the source string, one at a time #
# #
#########################################################################
WHILE i <= (str_len - old_len + 1)
#########################################################################
z [ Continued In Next Message... ]
---
~ SPEED 1.40 [NR] ~