Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get string from right hand side

Tags:

oracle

plsql

I am writing a query in Oracle.

I want to get a string from the right hand side but the string length is dynamic.

Ex:

299123456789

I want to get 123456789

substr(PHONE_NUMBERS,-X,Y)

X is different for each record.

I tried this:

substr(PHONE_NUMBERS,-length(PHONE_NUMBERS),Y)

and it didn't work..

How can I write this query?

like image 639
Mehmet Avatar asked Aug 09 '10 14:08

Mehmet


People also ask

What does RIGHT() do in sql?

SQL Server RIGHT() Function The RIGHT() function extracts a number of characters from a string (starting from right).

Is there a right function in Oracle SQL?

Oracle SQL doesn't have LEFT and RIGHT functions. They can be emulated with SUBSTR and LENGTH.

How do I get last 4 characters of a string in SQL?

To get the first n characters of string with MySQL, use LEFT(). To get the last n char of string, the RIGHT() method is used in MySQL.


5 Answers

If you want to list last 3 chars, simplest way is

 select substr('123456',-3) from dual;
like image 149
Sanjay Kattimani Avatar answered Oct 05 '22 15:10

Sanjay Kattimani


SQL> select substr('123456',-1,6) from dual;

S
-
6

SQL> select substr('123456',-6,6) from dual;

SUBSTR
------
123456

SQL> select substr('123456',-7,6) from dual;

S
-

If you watch above statements, 3 query gives null value as -7 > length('123456').

So check the length of CONT_PHONE_NUMBERS and PHONE_NUMBERS

Hope this helps you

like image 26
Bharat Avatar answered Oct 05 '22 15:10

Bharat


SQL> select substr('999123456789', greatest (-9, -length('999123456789')), 9) as value from dual;

VALUE
---------
123456789

SQL> select substr('12345', greatest (-9,  -length('12345')), 9) as value from dual;

VALUE
----
12345

The call to greatest (-9, -length(string)) limits the starting offset either 9 characters left of the end or the beginning of the string.

like image 39
Adam Musch Avatar answered Oct 05 '22 13:10

Adam Musch


I just found out that regexp_substr() is perfect for this purpose :)

My challenge is picking the right-hand 16 chars from a reference string which theoretically can be everything from 7ish to 250ish chars long. It annoys me that substr( OurReference , -16 ) returns null when length( OurReference ) < 16. (On the other hand, it's kind of logical, too, that Oracle consequently returns null whenever a call to substr() goes beyond a string's boundaries.) However, I can set a regular expression to recognise everything between 1 and 16 of any char right before the end of the string:

regexp_substr( OurReference , '.{1,16}$' )

When it comes to performance issues regarding regular expressions, I can't say which of the GREATER() solution and this one performs best. Anyone test this? Generally I've experienced that regular expressions are quite fast if they're written neat and well (as this one).

Good luck! :)

like image 26
TF Krog Avatar answered Oct 05 '22 15:10

TF Krog


substr(PHONE_NUMBERS, length(PHONE_NUMBERS) - 3, 4)
like image 4
Joe User Avatar answered Oct 05 '22 13:10

Joe User