• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • SQL Functions for Mashing Characters

    August 14, 2002 Timothy Prickett Morgan

    Hey, Howard:

    I have a field that has embedded ‘-‘ characters that I want to remove.

    For example, if the field contains 01-111-3345, I want to return it as 001113345.

    However, the translate function does not accept an empty string.

    Can you tell me how to remove characters from a string in SQL?

    — Mike

    Unfortunately, there is not an easy way to do this when you have more than one occurrence of the character in a string.

    However, a user-defined function using the Persistent Stored Modules (PSM), feature of DB2 is one way to get this done:

    CREATE function sqlbook.ELIMCHAR
    	(InString varchar(100),RepChar VARCHAR(1))
    	returns varchar(100)
    	LANGUAGE SQL
    BEGIN
    DECLARE OutStr varchar(100);
    DECLARE Spos INTEGER;
    SET OutStr=InString;
    SET spos = -1;
    WHILE spos<>0 DO
     SET Spos = locate(RepChar,OutStr,1); 
     IF Spos>1 AND spos<LENGTH(outstr)-1 THEN
      SET OutStr = substr(OutStr,1,Spos-1)||
          substr(OutStr,Spos+1,LENGTH(OutStr)-spos);
     ELSEIF Spos=LENGTH(outstr) THEN
      SET OutStr = substr(OutStr,1,spos-1);
     ELSEIF spos=1 THEN
      SET OutStr = substr(OutStr,2,LENGTH(outstr)-1);		
     END IF;
    END WHILE;
    RETURN OutStr;
    END
    

    This user-defined function uses the LOCATE function to find the occurrence of the string. If LOCATE returns 0, none of the conditions are executed and the loop exits. If LOCATE returns a position, first I determine if the position is greater than (>) one and less than (<) the length of the string, which indicates the character is somewhere in the middle of the string. If so, I use the returned position of the character as an argument to the substring function, which returns beginning and ending strings. Subsequently, the CONCAT operator (||) is used to meld the substrings together.

    If the position of the passed character is at the end of the string, I use the SUBSTR function to return the string minus the ending character. Finally, if the string is in the first position, I use SUBSTR to remove just the first character. The function loops until all occurrences of the character are replaced.

    Here’s one thing to note about this function: I used the VARCHAR data type for both the input string, (InString), the matching character, (RepString), and the return data type, (OutStr). This is because all CHAR data types can be promoted to VARCHAR automatically by SQL, but VARCHAR data types cannot be demoted to CHAR. Any strings you pass to the function will be seen as VARCHAR, so if the arguments are declared as CHAR, the function will not be found. Remember, SQL looks for the user-defined functions not only by name, but also ensures that the data types for the arguments match or are compatible.

    Here’s an example of how this could affect you. Say I coded the function in the following manner:

    CREATE function sqlbook.ELIMSTRING
    	(InString varchar(100), RepChar CHAR(1))
    	returns varchar(100)
    ...... more function stuff
    

    If I have a table called XYZ that has a column called ABCD, which is type CHAR(20), the following call will NOT work:

    SELECT ELIMSTRING(ABCD,'-') FROM XYZ
    

    This call will fail and indicate that the function ELIMSTRING is not found. It does not fail because column ABCD is a CHAR(20) and the function is coded to accept only VARCHAR(100) arguments in the first position. It will fail because the second argument, ‘-‘, is seen by DB2 as a VARCHAR data type and the function is declared to accept only CHAR(1) data types in the second argument. However, if I code the function with the following declaration it will work:

    CREATE function sqlbook.ELIMSTRING
    	(InString varchar(100), RepChar VARCHAR(1))
    	returns varchar(100)
    ...... more function stuff
    

    DB2 knows that a CHAR(20) column is compatible with a VARCHAR(100) column, so that passes the data-type test. Since DB2 sees the second argument as a VARCHAR(1) anyway, the second argument also passes the test and the proper function is “found”. If you think about it, it is kind of stupid to declare an argument as VARCHAR(1), but this is required if you want to pass your arguments as literals because DB2 will see the literal string as a VARCHAR, not a CHAR of the length of the literal.

    I hope this helps.

    — Howard

    Howard F. Arner, Jr. is Vice President of Client Server Development, Inc., in Jacksonville, Florida, and he is the author of “iSeries and AS/400 SQL at Work.” You can buy Howard’s book at www.sqlthing.com/books, or go there to get more information about DB2 on the AS/400.

    Sponsored By
    COMMON

    REGISTER FOR COMMON IN DENVER, OCT. 13-17

    Get the IT training you need by attending COMMON Users Group’s Fall 2002 IT Education Conference & Expo, October 13-17 in Denver. Early Bird registration is $1,150 until September 4.

    Choose from over 720 sessions and labs covering a wide range of industry topics. Also receive training from J.D. Edwards, MAPICS, and other vendors.

    Don’t miss out! Go to www.common.org

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: mgo_rc, Volume 2, Number 61 -- August 14, 2002

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Date and Time Functions in V5R1 RPG Confirm File Deletions in Qshell

    Leave a Reply Cancel reply

MGO Volume: 2 Issue: 61

This Issue Sponsored By

    Table of Contents

    • Using Timestamps as Unique Keys
    • Make an SQL UDF Return Null
    • SQL Functions for Mashing Characters

    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