Discussion:
Replace 1 part of a number with another!
(too old to reply)
Robert
2004-10-22 13:19:01 UTC
Permalink
Hi,

I know this is possable in Foxpro(6) but I have a numeric field in a FoxPro
table. It is 8 characters long. I wish to replace the 4th number with another
specific number.

i.e. the current number is between:

44072001 and 44072504

i wish to replace all the '7' at position 4 with a '3'

Any help would be appreciated

Rob
Olaf Doschke
2004-10-22 13:27:13 UTC
Permalink
Post by Robert
44072001 and 44072504
i wish to replace all the '7' at position 4 with a '3'
If really all numbers are in that range,
simply subtract 40000 from all...

I know, maths is hard...:-P

Bye, Olaf.
Robert
2004-10-22 13:37:06 UTC
Permalink
Hi Olaf,

I had done that before, but i felt there was a better way to do the relace.

Rob
Post by Olaf Doschke
Post by Robert
44072001 and 44072504
i wish to replace all the '7' at position 4 with a '3'
If really all numbers are in that range,
simply subtract 40000 from all...
I know, maths is hard...:-P
Bye, Olaf.
Olaf Doschke
2004-10-22 14:07:37 UTC
Permalink
Hi Rob,
Post by Robert
I had done that before, but i felt there was a better way to do the relace.
If you want it more generic, to be able to do this even if some numbers don't
have a 7 at 4th position, then you have to convert to a string (STR or TRANSFORM),
work with Stringfunctions and then reconvert to a number with VAL(). It could still be
one line of code, but why make it complicated if it is simple?

Does this look intuitive or good to you?
replace nfield with val(left(str(nfield,8),3)+"3"+right(str(nfield,8),4)) for substr(str(nfield,8),4,1)="7"

You could make it a bit shorter with Stuff():
replace nfield with val(Stuff(Str(nfield,8),4,1,"3")) for substr(str(nfield,8),4,1)="7"
but that's about it.

With chrtran(nfield,"7","3") you'd replace any 7 with a 3, so that's no solution.

replace nfield with nfield-40000 is just fine, especially because it's a one time task,
isn't it?

Bye, Olaf.
Fred Taylor
2004-10-22 15:18:58 UTC
Permalink
YourNewCharValue = STUFF(YourCharValue,4,1,"3")
--
Fred
Microsoft Visual FoxPro MVP
Post by Robert
Hi,
I know this is possable in Foxpro(6) but I have a numeric field in a FoxPro
table. It is 8 characters long. I wish to replace the 4th number with another
specific number.
44072001 and 44072504
i wish to replace all the '7' at position 4 with a '3'
Any help would be appreciated
Rob
Loading...