Using SQL to Update a Portion of a Field

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

From: Sharon Cannon To: Ted Holt

I know that with SQL it is very easy to update a field, but is it possible to substring part of a field and update that partial field?

From: Ted Holt To: Sharon Cannon

Certainly! You can use the SUBSTR operator to get a substring from a field. For example, to get bytes 3-7 of a field called MYNAME, use the expression SUBSTR(MYNAME,3,5).

You can update part of a character field by concatenating the parts you want to stay intact with the new value. Let's say you're going to replace the first character of a field called CITY with 'X' in file SOMEFILE. The following SQL statement could accomplish the task:

 UPDATE SOMEFILE SET CITY = 'X' CONCAT SUBSTR(CITY,2) WHERE ... 

It sure beats writing a quick-and-dirty program.

From: Sharon Cannon To: Ted Holt

I tried your suggestion and it worked perfectly! I wanted to use an SQL statement to update a flat (program-described) file containing one 80-byte field called DATA. I needed to change an amount field in positions 30 through 35 to contain all zeros. Here's the statement I used:

 UPDATE LMLIBR1/CREDITU SET DATA = SUBSTR(DATA,1,29) CONCAT '000000' CONCAT SUBSTR(DATA,36,45) 

It worked like magic!

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: