Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to replace char. field with numeric field padded with 0's

Status
Not open for further replies.

muckslush

Technical User
Feb 22, 2006
1
US
I have a .dbf table with several fields. I would like to replace a new char. field that I created (c, 10) with the values from a numeric field (n, 10), padding the first several places with "0"'s. I know how to do this when using 2 character fields, but can't get it right when trying with num>char. Thanks in advance.
 
Try this on one of the records:
REPLACE charfield WITH Padl(ALLTRIM(STR(numfield)), 10, '0')

If that's what you want, add ALL to the command:
REPLACE ALL charfield WITH Padl(ALLTRIM(STR(numfield)), 10, '0')

To replace every record.

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
When I have a problem like, rather than trying the replacement on a record, I just test it from the Command Window:

?PADL(ALLTRIM(STR(numfield)),10,'0')

If that gives me the right results, I can do the replace.

Tamar
 
I may be paranoid but I'd go one step further than Tamar and take a look at all the data first:
Code:
Select Padl(Alltrim(Str(numfield)),10,'0') As Dummy ;
  From myTable ;
  Order By Dummy
I can then have a quick read down the output to see that there's a nice consistent pattern. The Order By clause should throw any weird results to the top or bottom of the list.

I would also do a backup first.

Geoff Franklin
 
I avoid putting constants into the code like 10 since they are likely to change later. I'd like the code to adapt.

REPLACE ALL charfield WITH Padl(ALLTRIM(STR(numfield)), len(charfield), '0')

This also makes it obvious that 10 isn't some value picked out of thin air.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top