- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Can someone help in the extracting last three characters of a string...irrespective of the length of the string i should extract last 3 three into new variable......I am attaching the sample number
32-015-10101
32-015-11201
32-015-11501
32-015-11502
32-015-11503
32-015-11504
output should like this.....
101
201
501
502
503
504
Thanks in advance
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It just means to take the entire length of the variable, and then go back 2 spots from the end of the length. Then you take the next 3 values in the string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Not sure if you want the last 3 in character or numeric, but this should pull out the last 3 regardless of the length of the variable:
newvar=substr(oldvar,length(oldvar)-2,3);
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
it does help..can you let me know what does -2 mean in the code....
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It just means to take the entire length of the variable, and then go back 2 spots from the end of the length. Then you take the next 3 values in the string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It means that
length(var)-2 this means from the length of the variable you are going 2 places prior.
for ex: 32-015-10101
length (var)= 12
length(var)-1=11
length(var)-2=10 from 10th position you want to take three characters, which will give you the last 3 chars that is why its -2
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if the lenght of the var is 10 then we have to take the characters from 8,9 and 10th position.
substr(var, starting position, no of characters)
to get the starting position we will do length(var) - 2
if you want the last 5 characters then the same will be length(var)-4
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use the substring function
var2=substr(var1,length(var1)-2,3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if you want the result as character then
reqvar = substr(var,length(var)-2,3);
if you want the result as numeric then
reqvar = input( substr(var,length(var)-2,3),best3.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you can use reverse function
newvar=substr(reverse(strip(oldvar)),1,3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you use reverse function then the entire string will be reversed.
if you take the first 3 characters from the result then we will get the result which is reverse to the actual req output
the result will be like
101
102
105
.
.
.
but he wants the result like
101
201
501
.
.
.
So again we have to apply reverse for the final output to get the desired output
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content