fhg
Volume 6, Number 14 -- April 5, 2006

Change a Substring with SQL

Published: April 5, 2006

Hey, Ted:

I am trying to change part of a character field with an SQL UPDATE command. However, my SQL command gives me an invalid token error at the opening parenthesis of SUBSTR in the SET statement. Is there a rule that you can't use SUBSTR in a SET statement? If so, do you have a workaround?

--Dennis


What you're trying to do is reasonable, Dennis. After all, RPG and CL let you change part of a character field using their %SUBST and %SST functions. But SQL is different.

For the benefit of other readers, here is the SQL command Dennis sent to me.

update library/file 
   set substr(rpc,1,3) = char(comno)
 where comno in (015,016) and substr(rpc,1,3) = ' '

Dennis wants to replace the first three characters of the RPC field with whatever is in COMNO, but only if those characters are blank and only for records in companies 015 and 016. The solution is to replace the entire field, not just part of it, by selecting the parts you wish to keep and replacing the part you wish to update, like this:

update library/file
   set rpc = char(comno) || substr(rpc,4)
 where comno in (015,016) and substr(rpc,1,3) = ' '

In this example, the new value of RPC is the three company number digits concatenated to whatever is in positions four and following.

Thanks for the question, Dennis. This is a handy technique to know.

--Ted



Sponsored By
ADVANCED SYSTEMS CONCEPTS

SEQUEL can be used for virtually ALL data access functions on the iSeries.

A Windows-based user interface makes it easy to design queries and reports.

SEQUEL offers executive dashboards, drill-down data analysis and run-time prompts to deliver important iSeries data to managers and other non-technical users.

E-mail and FTP delivery let you deliver information to remote users and servers.

www.asc-iseries.com



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

California Software:  Migrate iSeries apps to Windows, Linux, or Unix
Computer Keyes:  Rapidly convert *SCS printer files into black and white or full color PDF documents
Bug Busters Software Engineering:  Quality software solutions for the iSeries since 1988

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement