• 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
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    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

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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