• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • REPLACE to Replace Characters in a String

    July 27, 2011 Skip Marchesani

    In V5R3, IBM simplified the manipulation of character strings with the implementation of the INSERT and REPLACE functions in SQL. INSERT allows the positional insertion of one or more characters in a string and REPLACE scans for all occurrences of a target string and overlays or replaces the target string with a replace string. INSERT was discussed in my previous article, and now I will discuss REPLACE.

    Prior to the implementation of REPLACE in V5R3, scanning for the existence of a target string and replacing it with another string was not very easy. A positional replacement could be performed by creating a complex SQL statement that split a string into the appropriate pieces with SUBSTR, omitting the character(s) to be replaced, and inserting the replacement characters with CONCAT.

    If the string needed to be scanned to search for a string to be replaced, an SQL procedure could be created–most likely using SUBSTR and CONCAT–that would find a target string and replace it with a replacement string.

    In V5R3, the REPLACE function turns a complex SQL statement into a simple one. The REPLACE function scans a source string searching for a target string, and replaces all occurrences of the target string with a replace string. And, the replace string can be longer or shorter that the target string. If the target string is not found in the source string, the source string is returned without any change in the result set.

    The syntax for the REPLACE function is simple:

    REPLACE(source_string, target_string, replace_string)
    

    To see how REPLACE works, let’s assume we have a name and address table that includes phone number with two hyphens and is defined as CHAR(12). The format of the phone number is AAA-EEE-NNNN; where AAA- is the area code, followed by a hyphen, EEE- is the phone exchange, followed by a hyphen, and NNNN is the phone number.

    Following is a SELECT statement that will return NAME, COMPANY, and PHONE, and use REPLACE to remove the two hyphens from PHONE.

    SELECT name, company, REPLACE(phone, '-', '') AS phone
    	FROM namemstr
    	ORDER BY name
    

    In the above SELECT statement the REPLACE searched for all occurrences of a hyphen and removed the hyphens, or replaced them with nothing. To tell REPLACE to replace the target string with nothing, two single quote marks must be used together with no space between them. Therefore, don’t make the mistake of thinking that the ”, the third operand or expression that follows the ‘-‘ in the REPLACE, is a double quote mark.

    Now let’s look at an example of using REPLACE to replace one or more characters in a string with an actual value instead of nothing. In the following table, the column name is 10 positions long, and we will replace all occurrences of a lowercase “k” with two uppercase Xs (XX).

    Nbr	Nam
    10	Ed
    20	Heikki
    30	John
    40	Mike
    50	Marcela
    60	Frank
    

    The SQL syntax to do the REPLACE is below and followed by the result of the REPLACE.

    SELECT  nbr,  nam,  REPLACE (nam, 'k', 'XX') AS replace
         FROM  emp  ORDER BY  nbr;
    
    Nbr	Nam	Replace
    10	Ed	Ed
    20	Heikki	HeiXXXXi
    30	John	John
    40	Mike	MiXXe
    50	Marcela	Marcela
    60	Frank	FranXX
    

    Remember that the replace string does not have to be the same length as the target string: it can be longer or shorter. When the replace string is not the same length as the target string, the length of the source string will be adjusted accordingly, but cannot exceed the maximum length for the data type of the source string.

    The length of the source string after a replace is the length of the replace string minus the length of the target string, multiplied by the number of occurrences of the target string, added to the original length of the source string.

    ((replace_string - target_string) * (occurrences_of_target) + 
    original_source_string_length
    

    REPLACE is case-sensitive and will differentiate between uppercase and lowercase characters when searching for the target string.

    REPLACE provides a very easy way to replace all occurrences of a target string in a source string with a replace string.

    Skip Marchesani retired from IBM after 30 years and decided to pursue a career as a consultant in the IBM i marketplace. He spent much of his IBM career working with the Rochester Development Lab on projects for S/38 and AS/400, and was involved with the development of the AS/400. Skip was part of the team that taught early AS/400 education to customers and IBM lab sites worldwide. Now recognized as an industry expert on DB2 for i (a.k.a. DB2/400), and author of the book “DB2/400: The New AS/400 Database,” Skip specializes in providing customized education for any area of the IBM i and AS/400, does database design and design reviews, and general IBM i, iSeries, and AS/400 consulting. He has been a speaker for user groups, technical conferences, and IBM i, iSeries, and AS/400 audiences around the world, and has received COMMON’s Distinguished Service Award. Skip Recently moved to Vermont and is now president of the Vermont Mid Range User Group (VTMUG). Send your questions or comments for Skip to Ted Holt via the IT Jungle Contact page.

    RELATED STORIES

    INSERT to Overlay Positions in a Character String

    SQL Implicit Cast of Character Strings and Numeric Values

    Use SQL to Remove Extra Spaces



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Computer Keyes

    Fax Directly from your IBM i

    KeyesFax is a full function automated IBM i fax system. Spooled files are burst by fax number and auto transmitted with overlays.  It combines both a send and receive facsimile processing system with a complete image package.

    The fax software will edit, send, receive, display, print, and track fax documents or images using any standard IBM i without additional expensive hardware, software or subscriptions.

    Computer Keyes has been developing Software Solutions since 1978!

    www.computerkeyes.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    looksoftware:  Integrate RPG applications and deploy to the iPad. Download the case study
    CNX:  Ready to develop true Web 2.0 applications? Valence Version 3.0 BETA 3 is now available
    System i Developer:  Join the Gurus at the RPG & DB2 Summit in St. Louis, October 17-19

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    XIV Clustered Disk Arrays Get More Oomph And Capacity Lean Mean Green Screens

    Leave a Reply Cancel reply

Volume 11, Number 22 -- July 27, 2011
THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
System i Developer

Table of Contents

  • REPLACE to Replace Characters in a String
  • Solving iSeries Access Data Transfer Problems and Detecting Disk Damage
  • Admin Alert: A Starter Program to Find Damaged Objects in i OS 6.1.1

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26
  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle