Reputation: 787
I have a variable with size 10. it stores a 10 size value like 'KUNAL12345'. How can I check the last 3 characters of the value to be numeric or not. In this case the last 3 character is 345 which is numeric value.
Upvotes: 2
Views: 1088
Reputation: 3202
You can use %check like this (i did not test)
dcl-c digits '0123456789';
dcl-s value char(10) inz('KUNAL12345');
if %check(%subst(value:8:3):digits) = 0;
// each of the three last characters is a digit character
endif;
or as @jtaylor says
if %checkr(value:digits) < 8;
// each of the three last characters is a digit character
endif;
Upvotes: 6
Reputation: 2473
you can use sql regexp
functions. The regex that matches 3 digits at end of a string is \d\d\d$
d text s 80a varying
d match s 80a varying
d i5 s 5i 0
d numMatches s 10i 0
/free
text = 'steve 7335' ;
exec sql
set :numMatches = regexp_count( :text, '\d\d\d$', 1 ) ;
if numMatches > 0 ;
endif ;
text = 'steve 733z3' ;
exec sql
set :match :i5 = regexp_substr( :text, '\d\d\d$', 1, 1 ) ;
if i5 = -1 ;
// not a match
endif ;
Upvotes: 2