I am trying to fetch an id from an oracle table. It's something like TN0001234567890345
. What I want is to sort the values according to the right most 10 positions (e.g. 4567890345
). I am using Oracle 11g. Is there any function to cut the rightmost 10 places in Oracle SQL?
-
Thanks a lot codaddict and Peter G for helping me. Its is working fine now :)– tismonOct 8, 2010 at 7:34
5 Answers
You can use SUBSTR function as:
select substr('TN0001234567890345',-10) from dual;
Output:
4567890345
-
4+1. Also, the third parameter is not required:
substr('TN0001234567890345',-10)
yields the same result. Oct 8, 2010 at 9:15 -
4Beware that if the string is less than 10 characters, it will return NULL instead of the whole string.– StammJan 12, 2018 at 11:02
-
@Stamm - See Briguy37's answer for a simple solution that handles it... it's really too bad that
substr()
doesn't automatically handle it... I'd definitely consider that to be a bug. Oct 4, 2018 at 15:33
codaddict's solution works if your string is known to be at least as long as the length it is to be trimmed to. However, if you could have shorter strings (e.g. trimming to last 10 characters and one of the strings to trim is 'abc') this returns null which is likely not what you want.
Thus, here's the slightly modified version that will take rightmost 10 characters regardless of length as long as they are present:
select substr(colName, -least(length(colName), 10)) from tableName;
-
1This should be the accepted answer. The double
reverse
will likely come out shorter, as this requires repeating the column name, so breakeven is if the column name is 3 characters... but still, I think this is easier to read. Oct 4, 2018 at 15:32 -
@ArtOfWarfare For ease of reading, I would think
select nvl(substr('TN0001234567890345', -10),'TN0001234567890345') from dual
would make even more sense. Suprised no one suggested that. Nov 15 at 13:22
Another way of doing it though more tedious. Use the REVERSE
and SUBSTR
functions as indicated below:
SELECT REVERSE(SUBSTR(REVERSE('TN0001234567890345'), 1, 10)) FROM DUAL;
The first REVERSE function will return the string 5430987654321000NT
.
The SUBSTR
function will read our new string 5430987654321000NT
from the first character to the tenth character which will return 5430987654.
The last REVERSE
function will return our original string minus the first 8 characters i.e. 4567890345
-
1
-
1
-
4If the string happens to be less than 10 characters long, you would logically want to return the whole string. The accepted answer returns NULL whereas this one works in that case. Actually, I had to use this solution at work because we have variable-length strings.– StammJan 12, 2018 at 11:01
-
SQL> SELECT SUBSTR('00000000123456789', -10) FROM DUAL;
Result: 0123456789
-
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.– YunnoschFeb 9, 2021 at 9:27
-
Also discuss the benefits this provides over existing answers, some of which seem to do more protection against special cases.– YunnoschFeb 9, 2021 at 9:28
Yeah this is an old post, but it popped up in the list due to someone editing it for some reason and I was appalled that a regular expression solution was not included! So here's a solution using regex_substr in the order by clause just for an exercise in futility. The regex looks at the last 10 characters in the string:
with tbl(str) as (
select 'TN0001239567890345' from dual union
select 'TN0001234567890345' from dual
)
select str
from tbl
order by to_number(regexp_substr(str, '.{10}$'));
An assumption is made that the ID part of the string is at least 10 digits.
-
Overcomplication and slowness due to regex could be acceptable, if it would solve the issue that the plain use of substr has (returning
null
for shorter string). Since it doesn't, this solution is for from optimal for this problem. Aug 16, 2019 at 7:38 -
Oh yes, I did, <strike>but I got no sense of humor</strike>, but I thought others (less experienced with Oracle or less proficient in English) might overlook it or misinterpret it, and take the answer for granted. Aug 16, 2019 at 12:32
-