ABL技术咨询:将单数字后缀作业编号补零后写入UD字段
Hey there! As someone new to ABL, I get how tricky these string manipulation tasks can feel at first. Let's walk through exactly how to convert those single-digit suffixes (like -0, -5) to two-digit format (-00, -05) and write the result to your UD field.
Step-by-Step Approach
The core idea is to split the job number into its prefix and suffix parts, format the suffix to always be two digits, then recombine them and update your UD field.
Working ABL Code Example
Here's a practical implementation you can adapt to your database table:
-- Define variables to hold our values DEFINE VARIABLE cOriginalJobNum AS CHARACTER NO-UNDO. DEFINE VARIABLE cPrefix AS CHARACTER NO-UNDO. DEFINE VARIABLE iSuffix AS INTEGER NO-UNDO. DEFINE VARIABLE cFormattedJobNum AS CHARACTER NO-UNDO. -- Example input (replace this with your actual field value from the database) cOriginalJobNum = "99999-0". -- Split the job number into prefix and suffix parts cPrefix = SUBSTRING(cOriginalJobNum, 1, INDEX(cOriginalJobNum, "-") - 1). iSuffix = INTEGER(SUBSTRING(cOriginalJobNum, INDEX(cOriginalJobNum, "-") + 1)). -- Format the suffix to 2 digits (automatically adds leading zero for values 0-9) cFormattedJobNum = cPrefix + "-" + STRING(iSuffix, "99"). -- Write the formatted number to your UD field -- Replace "YourTable" with your actual table name, and adjust the WHERE clause to target the right record FOR EACH YourTable WHERE YourTable.JobNum = cOriginalJobNum EXCLUSIVE-LOCK: ASSIGN YourTable.UDField = cFormattedJobNum. END.
Key Explanations
- Splitting the Job Number: We use
INDEX()to find the position of the-character, thenSUBSTRING()to pull out the prefix (everything before-) and the suffix (everything after). - Formatting the Suffix: The
STRING(iSuffix, "99")function converts the integer suffix to a string with exactly two digits—so0becomes00,9becomes09, and values like10or150stay as-is. - Updating the UD Field: The
FOR EACHloop locks the record (to avoid conflicts) and usesASSIGNto set the UD field to our formatted job number.
Optional: Add Error Handling
If you want to handle edge cases (like job numbers without a -), you can add a quick check:
IF INDEX(cOriginalJobNum, "-") > 0 THEN DO: -- Run the splitting/formatting logic here END. ELSE DO: -- Handle invalid job number format (log a message or skip the record) MESSAGE "Invalid job number format: " cOriginalJobNum VIEW-AS ALERT-BOX. END.
This should solve your problem perfectly—just tweak the table and field names to match your setup!
内容的提问来源于stack exchange,提问作者Chris Simpson

