%DEC Built-In Functions in rpgle
%DEC function can be used to convert Date, Time OR Timestamp to Decimal.
We use it in the form %DEC(date time or timestamp expression {:format})
The converted decimal value will have the number of digits like the date or time format that we mention here in 2nd parameter.
e.g. if the 1st parameter is a Date and 2nd paramter is *YMD, the converted decimal value will be YYMMDD. Similarly for *MDY, the converted decimal will be MMDDYY.
Example 1–%dec built-in function in rpgle
%DEC(date time or timestamp expression {:format})
Here 2nd parameter is optional. In below example we are using only 1st paramter.
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 DToday S D
0002.00 DToday1 S 8P 0
0003.00 DTodaysTime S Z
0004.00 DTodaysTime1 S 20P 0
0005.00 C
0006.00 C Eval Today = %date()
0007.00 C Today DSPLY
0008.00 C
0009.00 C* Convert date to decimal
0010.00 C
0011.00 C Eval Today1=%dec(Today)
0012.00 C Today1 DSPLY
0013.00 C
0014.00 C
0015.00 C TIME TodaysTime
0016.00 C TodaysTime DSPLY
0017.00 C* Convert TimeStamp to decimal
0018.00 C EVAL TodaysTime1=%dec(TodaysTime)
0019.00 C TodaysTime1 DSPLY
0020.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 2017-11-14
DSPLY 20171114
DSPLY 2017-11-14-17.29.21.523000
DSPLY 20171114172921523000
Example 2–%dec built-in function in rpgle
%DEC(date time or timestamp expression {:format})
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 DToday S D
0002.00 DToday1 S 8P 0
0003.00 C
0004.00 C Eval Today = %date()
0005.00 C Today DSPLY
0006.00 C
0007.00 C* Convert date to decimal in *YMD format
0008.00 C
0009.00 C Eval Today1=%dec(Today:*YMD)
0010.00 C Today1 DSPLY
0011.00 C
0012.00 C* Convert date to decimal in *USA format
0013.00 C
0014.00 C Eval Today1=%dec(Today:*USA)
0015.00 C Today1 DSPLY
0016.00 C
0017.00 C
0018.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 2017-11-14
DSPLY 171114
DSPLY 11142017
%DEC function is also used to convert numeric or character expression to packed decimal format.
It is used in the form - %DEC(Numeric expression :digits : dec pos)
We need to mention here the number of digits and the decimal position.
Example 3-%dec built-in function in rpgle
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 DChar001 S 10A
0002.00 DDec001 S 7S 2
0003.00 C
0004.00 C Eval Char001 = '-2017.28'
0005.00 C
0006.00 C* Convert CHAR to decimal(7,2)
0007.00 C Eval Dec001= %dec(Char001:7:2)
0008.00 C
0009.00 C* Do some operation on Decimal
0010.00 C Eval Dec001= Dec001 - 1000.17
0011.00 C
0012.00 C* Convert to CHAR and display
0013.00 C Eval Char001= %CHAR(Dec001)
0014.00 C Char001 DSPLY
0015.00 C
0016.00 C SETON LR
****************** End of data ********************************************
Output
DSPLY -3017.45