Extracting Date from a string

FishermanFisherman Member Posts: 456
I've almost got a dataport working here for my EDI feed, but I'm having trouble parsing the date.

Starting at position 13, and extending for 8 characters, there is a date field in the file with a format of 'YYYYMMDD'.

I have tried to use the following syntax to extract the date, but the result I'm getting is ValidDemandDate (global variable) is FALSE.

Can anyone troubleshoot this one line for me?
ValidDemandDate := EVALUATE(DemandDate,COPYSTR(EDIRecord.RecordData,13,8));

Comments

  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Strange. It looks perfect to me. Maybe you should drop the left part of the line and then EVALUATE will throw an error message and then it will take us closer to what's wrong.

    One thing is sure. EVALUATE expect dates in the format set in your Territorial and Local Settings in Windows. I think it will be the reason.
  • FishermanFisherman Member Posts: 456
    Could be. I'll try that and see.

    Is there any way to use FORMAT to extract a date like this?
  • zeninolegzeninoleg Member Posts: 236
    I had the same problem with my EDI files, I had to write a small function to rearrange the string before EVALUATE
    Best Regards,
    Oleg
  • DenSterDenSter Member Posts: 8,307
    edited 2006-08-03
    FORMAT is used mostly to format a date into the desired format, I don't think it will work the other way aorund.

    try this:
    MyDateString := COPYSTR(EDIRecord.RecordData,13,8);
    EVALUATE(YearInt,COPYSTR(MyDateString,1,4));
    EVALUATE(MonthInt,COPYSTR(MyDateString,5,2));
    EVALUATE(DayInt,COPYSTR(MyDateString,7,2));
    MyValidDate := DMY2DATE(DayInt,MonthInt,YearInt);
    
    Where YearInt, MonthInt and DayInt are integers. I have not tested this, but I am pretty sure it'll work.

    Of course you would ideally put this in a function with the MyDateString as a parameter and a date as return value.
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Let's look at the error message first. If it is about the date format, then you can

    1) Set the Terrotorial Settings in Windows on that machine

    2) Write a function (an ugly bunch of COPYSTR's) that creates a string that is in the same format the Territorial Settings typically expect.

    FORMAT isn't useful in this case, because it works vice versa, it creates a string out of something else.
  • FishermanFisherman Member Posts: 456
    Ahh... ok.

    I'll do that and post the results.

    I thought I could nest a format() call inside of EVALUATE() to rearrange the string before passing into the evaluate function.

    Thanks! I'll you know the results.
  • zeninolegzeninoleg Member Posts: 236
    That is what I had to do(just found it):
    FormatDate(DateTXT : Text[8]) rDate : Date
    IF NOT EVALUATE(rDate, COPYSTR(DateTXT, 7,2)+COPYSTR(DateTXT,5,2)+COPYSTR(DateTXT,3,2)) THEN
      rDate := WORKDATE;
    
    I was getting DDMMYY from YYYYMMDD. Hope this helps :D
    Best Regards,
    Oleg
  • FeldballeFeldballe Member Posts: 26
    DenSter wrote:
    FORMAT is used mostly to format a date into the desired format, I don't think it will work the other way aorund.

    try this:
    MyDateString := COPYSTR(EDIRecord.RecordData,13,8);
    EVALUATE(YearInt,COPYSTR(MyDateString,1,4));
    EVALUATE(MonthInt,COPYSTR(MyDateString,5,2));
    EVALUATE(DayInt,COPYSTR(MyDateString,7,2));
    MyValidDate := DMY2DATE(DayInt,MonthInt,YearInt);
    
    Where YearInt, MonthInt and DayInt are integers. I have not tested this, but I am pretty sure it'll work.

    Of course you would ideally put this in a function with the MyDateString as a parameter and a date as return value.

    One tiny problem though. This code only works if datevalues are not null. That happens sometimes, so you would have to test for either Yearint, Monthint and Dayint not being null or zero. But then again, it goes without saying :whistle:
  • DenSterDenSter Member Posts: 8,307
    The beauty of posting code snippets is that you don't have to develop a working solution, so I don't have to worry about error handling and that sort of thing. The question was how to evaluate a date value, and that's what the code snippet does :).
  • David_CoxDavid_Cox Member Posts: 509
    Why Not Just:

    IF EVALUATE(MyDate,STRSUBSTNO('%1/%2/%3',
    COPYSTR(EDIRecord.RecordData,19,2),
    COPYSTR(EDIRecord.RecordData,17,2),
    COPYSTR(EDIRecord.RecordData,13,4)))THEN;

    This will allow for an empty string as well, with the IF statement :D

    I have not tested this bit of code but it will work!
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
Sign In or Register to comment.