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

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    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

  • 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
  • You Can Now Get IBM Tech Support For VS Code For i
  • Price Cut On Power S1012 Mini Since Power S1112 Ain’t Coming Until 2026
  • IBM i: Pro and Con
  • As I See It: Disruption
  • IBM i PTF Guide, Volume 27, Number 30

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