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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numeric to date conversion

Status
Not open for further replies.

youwannawhat

Programmer
Oct 3, 2001
43
0
0
US
I've imported a file into Foxpro and it has a date field as an 8-digit numeric(ie 20011201). I need to convert this to a standard date format{12/01/2001}. I've done this once before, but I can't remember how. I thought I had to actually write a conversion program, changing the numeric to a string and then parsing it off into day, month, year. But I don't remember how I pulled it back into a date.

Any help would be great.

Thanks

Paul
 
How about
nX = 20011202

mydate = CTOD(STR(nX,5,2)+STR(nX,7,2)+STR(nX,1,4))

 
Instead of CTOD() - which is dependent on VFP / system date settings, I'd use the DATE() function. First add a new column in your table that's a type DATE field. Then you could use a REPLACE to convert the date, then remove the numeric column from the table. e.g.
REPLACE ALL myddate WITH ;
DATE(int(mynumdate / 10000), ;
MOD(int(mynumdate / 100), 100), ;
MOD(mydatefield, 100))

Rick
 
If the date string is always 8 characters/numbers, in CCYYMMDD format and you are using vfp 6.0 or greater:


tmpFormat = transform(20021231, "@R ####,##,##")
newDate = date(&tmpFormat)

Note that
tmpFormat = transform(20021231, "@R 9999,99,99")
also works

Also, instead of importing into one field, break the field into three on import, YearPart, MonthPart, DayPart, then you can use the date function in one step:

Date(YearPart, MonthPart, DayPart) will provide the date for the three fields. A simple query to the new field:

Replace all NewDate with Date(YearPart, MonthPart, DayPart)
 
Thanks, everybody. Rick's solution did the trick. I'm ashamed to admit I was several dozens of lines into coding this with multiple variables, arrays, etc.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top