• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: String Manipulation Using SQL

    October 24, 2022 Mike Larsen

    Recently, I was working with an employee file where first and last name were concatenated with a comma separator. A requirement of the project was to parse the name and populate a different table where first and last name are separate columns. I won’t address the design of the employee file, but I will show how I completed the request.

    In Figure 1, I use the position string function via SQL to extract the first name.

     

     

    Figure 1. Extract first name

     

    In this example, I use the trim, substring, and posstr functions to position to the first character following the comma. In this case, it positions to the first name. For illustration purposes, I’ve hard-coded the strings, but in a production process you’d want to use variables. When I run this example in ACS, I get the result shown in Figure 2.

     

    Figure 2. Result of extracting first name

     

    The next step is to extract the last name as shown in Figure 3.

     


    Figure 3. Extract last name

     

    This statement works much the same as extracting the first name, but here I specify that I want to start the substring from the first position of the string and end the substring at the position prior to the comma. When I run this, I get the result shown in Figure 4.

     

    Figure 4. Result of extracting last name

     

    Now that I have the first and last names, I’ve decided to take this a step further. I thought it would be nice to convert the first and last names to proper case. Again, using SQL, I use the statement shown in Figure 5.

     

     

    Figure 5. Convert first name to proper case

     

    Here, I convert the first letter of the first name to upper case and concatenate it with the first name starting from position 2 in lower case to form the first name in proper case. The result is shown in Figure 6.

     

    Figure 6. Result of converting first name to proper case 

     

    I use the same statement to convert the last name to proper case (Figure 7).

     


    Figure 7. Convert last name to proper case

     

    The result of this statement is shown in Figure 8.

     

    Figure 8. Result of converting last name to proper case

     

    I often use SQL in my RPG programs where it makes sense. I have used the techniques described in this article several times in various projects since working with them over the past few years and have added them to my toolbox.

    Mike Larsen is a director of information technology at Auburn Pharmaceutical and an associate of Central Park Data Systems. He has been working with IBM i systems for over 20 years, and specializes in RPG, CL, and SQL and recently has been working with PHP and Python. Current projects have given Mike the opportunity to work with generating and parsing XML and JSON from SQL and consuming SOAP and REST web services. Although his main area of expertise is on IBM i, Mike has a passion for learning other languages and how he can integrate other platforms with IBM i.

    RELATED STORIES

    Guru: Regular Expressions, Part 1

    Guru: Regular Expressions, Part 2

    Guru: Debugging SQL Stored Procedures With ACS

    Guru: Creating PDF Documents With Python

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, ACS, FHG, Four Hundred Guru, IBM i, RPG, SQL

    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

    Power Systems Revenues Look To Grow In 2022 Finally, IBM i Cloud Dev Instances That Are Powerful And Cheap

    One thought on “Guru: String Manipulation Using SQL”

    • Jim says:
      October 24, 2022 at 10:05 am

      Mike–enjoyed your concise explanation of converting “Last, First” names. Do you have a similar technique for pulling a jobname out of the jobnumber/jobuser/jobname field that is frequently used in IBM i Services? Most of our jobs are run by the same user, so I find it easier to just substring everything after the jobnumber, but occasionally it would be nice to have a straightforward way of getting just the jobname. Thanks!

      Reply

    Leave a Reply Cancel reply

TFH Volume: 32 Issue: 71

This Issue Sponsored By

  • Fresche Solutions
  • CloudFirst
  • LANSA
  • WorksRight Software
  • DRV Technologies
  • LANSA

Table of Contents

  • Inside IBM i’s New Geospatial Functions For Db2
  • Finally, IBM i Cloud Dev Instances That Are Powerful And Cheap
  • Guru: String Manipulation Using SQL
  • Power Systems Revenues Look To Grow In 2022
  • IBM i PTF Guide, Volume 24, Number 43
  • Use A Pathfinder And Choose Your Path To Modernize Your IBM i

Content archive

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

Recent Posts

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

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