• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Use SQL to Remove Extra Spaces

    October 11, 2006 Hey, Ted

    The SQL sorting tip you published on September 13 was a good one. I had seen a similar tip written by Craig Mullins in Quest Software’s Pipeline Newsletter. Mullins had two other interesting tips on the same page. I thought your readers might like to see them as well.

    –Tom

    The Web page to which Tom refers is at http://www.quest-pipelines.com/newsletter-v7/0606_F.htm. Craig Mullins uses the technique about which I wrote to sort data on three-letter day abbreviations.

    Another tip he presents involves using the REPLACE function to remove extra spaces within a character string. IBM added the REPLACE function to SQL in V5R3.

    I ran the following query to see what would happen, and it worked like a charm.

    select name,                                                    
           replace(replace(replace(name,' ','<>'),'>',' ')
    from qtemp/mydata
    

    Here’s what I saw:

    NAME                  REPLACE  
    Joe Smith             Joe Smith
    Joe  Smith            Joe Smith
    Joe   Smith           Joe Smith
    Joe    Smith          Joe Smith
    Joe         Smith     Joe Smith
    Joe       Smith       Joe Smith
    

    So how does it work? The innermost REPLACE changes all blanks to a less-than greater-than pair. So, if there are three spaces between Joe and Smith, the innermost REPLACE returns Joe<><><>Smith.

    The middle REPLACE changes all greater-than less-than pairs to the empty string, which removes them. Joe<><><>Smith becomes Joe<>Smith.

    The outer REPLACE changes all less-than greater-than pairs to a single blank. Joe<>Smith becomes Joe Smith. Clever!

    You do not have to use the less-than and greater-than symbols. Any two characters that are not used in the field will work.

    As for the other technique, I did not quite catch Craig Mullins’ drift, but maybe it was the example he gave.

    –Ted

    RELATED STORY:

    Unusual But Logical SQL Sorting

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Rocket Software

    Unlock the full potential of your data with Rocket Software. Our scalable solutions deliver AI-driven insights, seamless integration, and advanced compliance tools to transform your business. Discover how you can simplify data management, boost efficiency, and drive informed decisions.

    Learn more today.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    ASNA:  Monarch is a better way to modernize your RPG applications
    Bytware:  StandGuard Network Security 3.0, the next generation of System i security
    COMMON:  Join us at the Spring 2007 conference, April 29 – May 3, in Anaheim, California

    COMMON Picks Dufault as New President IBM Uncloaks Power6 Chip Details

    Leave a Reply Cancel reply

Volume 6, Number 37 -- October 11, 2006
THIS ISSUE SPONSORED BY:

Advanced Systems Concepts
WorksRight Software
Guild Companies

Table of Contents

  • CL Odds and Ends
  • Use SQL to Remove Extra Spaces
  • Admin Alert: The Ins and Outs of Loading Licensed Program Products

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