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

    Embrace VS Code for IBM i Development

    The IBM i development landscape is evolving with modern tools that enhance efficiency and collaboration. Ready to make the move to VS Code for IBM i?

    Watch this webinar where we showcase how VS Code can serve as a powerful editor for native IBM i code and explore the essential extensions that make it possible.

    In this session, you’ll discover:

    • How ARCAD’s integration with VS Code provides deep metadata insights, allowing developers to assess the impact of their changes upfront.
    • The role of Git in enabling seamless collaboration between developers using tools like SEU, RDi, and VS Code.
    • Powerful extensions for code quality, security, impact analysis, smart build, and automated RPG conversion to Free Form.
    • How non-IBM i developers can now contribute to IBM i projects without prior knowledge of its specifics, while ensuring full control over their changes.

    The future of IBM i development is here. Let ARCAD be your guide!

    Watch the replay now!

    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

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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