STRING_TO_DATE function exception

tvm
2019-03-27
2019-03-28
  • tvm - 2019-03-27

    using the STRING_TO_DATE function, I have found that an improperly formatted string could cause an Access Violation exception in the program.

    Β  Β DateAsString:Β  Β Β  Β STRING:= '2019-03-04';
    Β  Β DateAsDate:Β  Β Β  Β DATE;
    Β  Β DateAsDate:= STRING_TO_DATE(CONCAT('DATE#', DateAsString));
    

    The above code works as expected, however if DateAsString becomes 'End Date', the function receives a string that looks like 'DATE#End Date', and will cause an exception.

    Is there any way to pre-validate the string before converting it?

     
  • e.kislov - 2019-03-28

    Hi. You can use FSTRING_TO_DT from OSCAT library (with additional TO_DATE conversion).
    https://store.codesys.com/oscat-basic.html

    In case of invalid input string (like 'End Date') you just got 'NULL' date (1970-1-1) without any exception.

    IMG: 28

     
  • mjuzwiak - 2019-03-28

    Other option is to manually check if passed string is matching required format:
    1. Prepare function to check if single character in string is a character number (return true/false)
    2. Prepare function to check if character is dash (return true/false)
    3. Run this functions for all characters, one by one
    4. If at least one call return false, do not perform conversion

     

Log in to post a comment.