Getting string from rightside 10 char - sql

My table is in oracle toad. teh table contain column name phone number varchar2 datatype.it contain set of phonenumbers. some numbers are more than 10 char. i want to filter that number from right side 10 char.
data's in the table
-------------------
phone number
9948184759
9948220955
994823298612
9948249815
99482599971234
9948277935
9948288258
99483015076789
9948335085
9948337552
9948338134
the above column values are phone numbers.but some numbers are more than 10 char length
that numbers are
----------------
994823298612
99482599971234
99483015076789
expected output for the above numbers
----------------------------------------
4823298612
2599971234
3015076789
Help me to do this? am new to oracle toad

Simpler:
select substr(phone_number, -10) from ...

You can achieve that by using Substr function for example
with T1 as
(
select 99482599971234 n from dual union all
select 99483015076789 n from dual union all
select 994823298612 n from dual
)
select substr(n, Length(n) - 9, 10) nn
from t1
Nn
-------------------
4823298612
2599971234
3015076789

Related

Number pattern matching for a range in SQL

I want to select columns in Oracle: Code and Number based on the condition,
If number is any of the following return “E”: ###7890001 through ###7890999
where #(first 3 digits) could be any digit. These numbers are saved as string values so I need to cast them in appropriate data type too.
The data returned would look like this:
Code Number
E 2347890001
E 9567890456
E 5647890999
Thanks in adavnce!!
Assuming your problem description is correct, your inputs are always 10-digit strings, and you must return code E if and only if the four digits in the middle are 7890. Right? If so, the simplest solution is to use the LIKE condition. Note that number is a reserved keyword, so it can't be used as column name (I hope your column name is not really number, is it?)
The string in the LIKE condition is hard to read; it consists of three underscore characters, then 7890, then three more underscore characters. The underscore stands for exactly one character (any character).
with
simulated_data (num) as (
select '1234567890' from dual union all
select '3337890456' from dual union all
select '7897890455' from dual union all
select '9998887774' from dual
)
select num, case when num like '___7890___' then 'E' end as code
from simulated_data
order by num
;
NUM CODE
---------- ----
1234567890
3337890456 E
7897890455 E
9998887774
You can use to_number with mod and on conversion error (oracle 12.2 or higher) as following:
Select 'E' as code,
number
From your_table
Where MOD(TO_NUMBER(number DEFAULT -1 ON CONVERSION ERROR), 10000000)
between 7890001 and 7890999;
Cheers!!
This should work:
where code = 'E' and
substr(number, -7) between '7890001' and '7890999'
EDIT:
select (case when substr(number, -7) between '7890001' and '7890999' then 'E' end) as code

Remove 2 characters in oracle sql

I have a column that contains 12 digits but user wants only to generate a 10 digits.
I tried the trim, ltrim function but nothing work. Below are the queries I tried.
ltrim('10', 'column_name')
ltrim('10', column_name)
ltrim(10, column_name)
For example I have a column that contains a 12 digit number
100000000123
100000000456
100000000789
and the expected result I want is
0000000123
0000000456
0000000789
To extract the last 10 characters of an input string, regardless of how long the string is (so this will work if some inputs have 10 characters, some 12, and some 15 characters), you could use negative starting position in substr:
substr(column_name, -10)
For example:
with
my_table(column_name) as (
select '0123401234' from dual union all
select '0001112223334' from dual union all
select '12345' from dual union all
select '012345012345' from dual
)
select column_name, substr(column_name, -10) as substr
from my_table;
COLUMN_NAME SUBSTR
------------- ----------
0123401234 0123401234
0001112223334 1112223334
12345
012345012345 2345012345
Note in particular the third example. The input has only 5 digits, so obviously you can't get a 10 digit number from it. The result is NULL (undefined).
Note also that if you use something like substr(column_name, 3) you will get just '345' in that case; most likely not the desired result.
try to use SUBSTR(column_name, 2)

Find the accent data in table records

In a table, I have a column that contains a few records with accented characters. I want a query to find the records with accented characters.
If we have records like as below:
2ème édition
Natália
sravanth
query should pick these records:
2ème édition
Natália
You can use the REGEXP_LIKE function along with a list of all the accented characters you're interested in:
with t1(data) as (
select '2ème édition' from dual union all
select 'Natália' from dual union all
select 'sravanth' from dual
)
select * from t1 where regexp_like(data,'[àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ]');
DATA
--------------
2ème édition
Natália
The ASCIISTR function would be another way to find accented characters
ASCIISTR takes as its argument a string, or an expression that
resolves to a string, in any character set and returns an ASCII
version of the string in the database character set. Non-ASCII
characters are converted to the form \xxxx, where xxxx represents a
UTF-16 code unit.
So you can do something like
SELECT my_field FROM my_table
WHERE NOT my_field = ASCIISTR(my_field)
Or to re-use the demo from the accepted answer:
with t1(data) as (
select '2ème édition' from dual union all
select 'Natália' from dual union all
select 'sravanth' from dual
)
select * from t1 where data != asciistr(data)
which would output the 2 rows with accents.
with t1(data) as (
select '2ème édition' from dual union all
select 'Natália' from dual union all
select 'sravanth' from dual
)
select * from t1 where REGEXP_like(ASCIISTR(data), '\ \ [[:xdigit:]]{4}');
DATA
--------------
2ème édition
Natália
Way harder than it seems on the surface as there is more than one way to create an accent. What I do is have a mirror column I call clean and scrub out all the accents on load.
See this question I asked some time ago normalized string

SQL How to extract numbers from a string?

I am working on a query in SQL that should be able to extract numbers on different/random lenght from the beginning of the text string.
Text string: 666 devils number is not 8888.
Text string: 12345 devils number is my PIN, that is 6666.
I want to get in a column
666
12345
Use a combination of Substr & instr
SELECT Substr (textstring, 1,instr(textstring,' ') - 1) AS Output
FROM yourtable
Result:
OUTPUT
666
12345
Use this if you have text at the beginning e.g. aa12345 devils number is my PIN, that is 6666. as it utilises the REGEXP_REPLACE function.
SELECT REGEXP_REPLACE(Substr (textstring, 1,instr(textstring,' ') - 1), '[[:alpha:]]','') AS Output
FROM yourtable
SQL Fiddle: http://sqlfiddle.com/#!4/8edc9/1/0
This version utilizes a regular expression which gives you the first number whether or not it's preceded by text and does not use the ghastly nested instr/substr calls:
SQL> with tbl(data) as (
select '666 devils number is not 8888' from dual
union
select '12345 devils number is my PIN, that is 6666' from dual
union
select 'aa12345 devils number is my PIN, that is 6666' from dual
)
select regexp_substr(data, '^\D*(\d+) ', 1, 1, null, 1) first_nbr
from tbl;
FIRST_NBR
---------------------------------------------
12345
666
12345
SQL>

PLSQL Getting values from string

I have this varchar2(2000) string:
id=100\nid2=0\nid3=0\dtext='more Text'
and I want to get only the values e.g. more Text or 0 (id3).
I was trying to use a customized SPLIT function, where separator is \n but this only returns me for example id3=0 (in this case I need '0' as result).
How can I do this more efficient?
and I want to get only the values e.g. more Text.
Simply use SUBSTR and INSTR
SQL> WITH DATA AS
2 ( SELECT q'[id=100\nid2=0\nid3=0\dtext='more Text']' str FROM dual
3 )
4 SELECT SUBSTR(str,instr(str, '''')+1,LENGTH(SUBSTR(str,instr(str, '''')))-2) str
5 FROM DATA
6 /
STR
---------
more Text
SQL>
You could get all the values with something like this:
WITH DATA AS
(SELECT q'[id=100\nid2=0\nid3=0\ndtext='more Text']' str FROM dual)
SELECT replace(substr(regexp_substr(str,'(=.+?\n)|(=.+?$)',1,level),2),'\n') v
FROM DATA
CONNECT BY LEVEL <= LENGTH(regexp_replace(str,'([^=]+=.+?\n)|([^=]=.+?$)'))