DCL-S str CHAR(20) INZ(‘Hello World’);
DCL-S subStr CHAR(5);
subStr = %SUBST(str:1:5); // Result is ‘Hello’
Date and Time Functions
%DATE: Converts a character, numeric, or timestamp value to a date.
rpg
DCL-S date DATE;
date = %DATE(‘2024-12-23’); // Converts to date value
%TIMESTAMP: Converts a character or numeric value to a timestamp.
rpg
DCL-S timestamp TIMESTAMP;
timestamp = %TIMESTAMP(‘2024-12-23-20.30.00.000000’); // Converts to timestamp
%DIFF: Calculates the difference between two date, time, or timestamp values.
rpg
DCL-S startDate DATE INZ(D’2024-12-01′);
DCL-S endDate DATE INZ(D’2024-12-23′);
DCL-S daysDiff INT(10);
daysDiff = %DIFF(endDate:startDate:*DAYS); // Result is 22
Mathematical Functions
%ABS: Returns the absolute value of a numeric expression.
rpg
DCL-S num INT(10) INZ(-123);
DCL-S absNum INT(10);
absNum = %ABS(num); // Result is 123
%ROUND: Rounds a numeric value to the nearest integer.
rpg
DCL-S num FLOAT(8) INZ(123.456);
DCL-S roundedNum INT(10);
roundedNum = %ROUND(num); // Result is 123
%REM: Returns the remainder of a division operation.
rpg
DCL-S num1 INT(10) INZ(10);
DCL-S num2 INT(10) INZ(3);
DCL-S remainder INT(10);
remainder = %REM(num1:num2); // Result is 1
Miscellaneous Functions
%LOOKUP: Searches for an element in an array.
rpg
DCL-S array CHAR(10) DIM(3) INZ((‘A’:’B’:’C’));
DCL-S index INT(10);
index = %LOOKUP(‘B’:array); // Result is 2 (position of ‘B’ in the array)
%EOF: Indicates end-of-file for a file operation.
rpg
IF %EOF(fileName);
// Handle end-of-file condition
ENDIF;
These are just a few examples of the built-in functions available in RPGLE. They help simplify coding tasks and make programs more readable and maintainable. If you have any specific scenarios or functions in mind, feel free to ask for more details!