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

    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

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

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