Subject Re: Replacing a substring
From Ken Mayer <dbase@nospam.goldenstag.net>
Date Sun, 23 Jul 2023 12:53:23 -0700
Newsgroups dbase.getting-started

On 7/23/2023 12:08 PM, Charlie wrote:
> I am wondering how to change text like LI10000 TO LH0000?
>
> I have tried this:
> USE MASTER.DBF
> REPLACE substr(part_no,1,2) with "LH" for mfg = "LIONEL H O SCALE"
>
> But come back with error 'expecting with'

The dBASE REPLACE command assumes you're replacing the full contents of
the field, so there may be an issue there. I don't think you can do what
you're trying that easily. I could be wrong (and I expect if I am,
Mervyn will pop in with something).

You might want to try the SQL UPDATE SET command, but am not sure it
would work either, because local SQL wouldn't understand the SUBSTR()
function.



A test in the Command Window:


? cPart = "LI10000"
cPart = "LI10000"
? stuff( cPart, 1,2, "LH" )

Returns:

LH10000

Which is what I assume you need (a typo in your original message).

However again, I don't think the REPLACE command can handle this.

A suggestion:

USE MASTER.DBF // might want to back up the data before trying this
set filter to mfg="LIONEL H O SCALE"
go top
scan // loop through the filtered data
    cPart = part_no // get value from the field
    cPart2 = stuff( cPart, 1,2, "LH" ) // replace first two letters with
"LH"
    replace part_no with cPart2 // updated version
endscan

> Also the field part_no is indexed.  Will I need to re-index the table if I can get this done?

The index should automatically update ...

Ken

--
*Ken Mayer*
Ken's dBASE Page: http://www.goldenstag.net/dbase
The dUFLP: http://www.goldenstag.net/dbase/index.htm#duflp
dBASE Books: http://www.goldenstag.net/dbase/Books/dBASEBooks.htm
dBASE Tutorial: http://www.goldenstag.net/dbase/Tutorial/00_Preface.htm
dBASE Web Tutorial: http://www.goldenstag.net/dbase/WebTutorial/00_Menu.htm
dBASE DOS to Windows Tutorial:
http://www.goldenstag.net/dbase/DtoWTutorial/00_Menu.htm