Oracle Query - Get only strings in the select field - sql

Maybe this sounds a little bit crazy, but I need to come up with a query to retrieve only letters out of an alphanumeric field.
For example:
TABLE
1234ADD
3901AC
1812OPA
82711AUU
RESULTS EXPECTED
ADD
AC
OPA
AUU
Thank you!

Looks like you only want to remove numbers. You can use REGEXP_REPLACE for that in 10g or 11g:
SELECT REGEXP_REPLACE( your_column, '[0-9]*', '' ) FROM your_table;

SELECT REGEXP_REPLACE('1234ADD 3901AC 1812OPA 82711AUU', '[0-9]', '')
FROM dual

Try
SELECT TRANSLATE('1234ADD 3901AC 1812OPA 82711AUU', 'A1234567890', 'A') FROM dual;
and in general see: http://www.psoug.org/reference/translate_replace.html

Related

Regex: how to get the text between a few colons?

So, i have a lot of strings like the ones below in my database:
product1:1stparty:single_aduls:android:
product2:3rdparty:married_adults:ios:
product3:3rdparty:other_adults:android:
I need a regex to get only the text after the product name and before the device category. So, in the first line I'd get 1stparty:single_aduls, in the second 3rdparty:married_adults and in the third 3rdparty:other_adults. I'm stuck and can't find a way to solve that. Could anyone help me please?
As a regular expression, you can use:
select regexp_extract('product1:1stparty:single_aduls:android:', '^[^:]*:(.*):[^:]*:$')
This returns every after the first colon and before the penultimate colon.
We can try using REGEXP_REPLACE here:
SELECT REGEXP_REPLACE(val, r"^.*?:|:[^:]+:$", "") AS output
FROM yourTable;
This approach removes either the leading ...: or trailing :...: from the column, leaving behind the content you want. Here is a demo showing that the regex replacement is working:
Demo
You can also use standard split function and access result array element by index, which is quite clear to read and understand.
with a as (
select split('product1:1stparty:single_aduls:android:', ':') as splitted
)
select splitted[ordinal(2)] || ':' || splitted[ordinal (3)] as subs
from a
Consider below example
with your_table as (
select 'product1:1stparty:single_aduls:android:' txt union all
select 'product2:3rdparty:married_adults:ios:' union all
select 'product3:3rdparty:other_adults:android:'
)
select *,
(
select string_agg(part, ':' order by offset)
from unnest(split(txt, ':')) part with offset
where offset in (1, 2)
) result
from your_table
with output

Replacing Special and Unicode Char in PostgresSQL

I am using PostgreSQL. Data in the table looks like below there is ''. I would like to remove '' with the empty value.
Query:
select 'Patriots Colony Family Monthly'
Actual Result Screenshot:
Expected result:
Abcd
You can use regex_replace to remove non alphanumeric chars
select regexp_replace(yourColumn, '[^[:alnum:]]', ' ', 'g')
from yourTable;
You can also do like this
select regexp_replace(yourColumn, '[^a-zA-Z0-9]+',' ','g')
from yourTable;

Splitting of a String using query

I wrote a query which splits a string and show me as a value I want using SUBSTR
SELECT SUBSTR ('imagelocation/r1.jpg', 15) AS image_location FROM dual
I am getting the output as r1.jpg but I only want the value to come as r1. Please help
If you want something more universal, regexp_replace might be of use.
SELECT
regexp_replace('imagelocation/r1.jpg','^[^/]*/([^.]+).*$','\1') AS image_location
FROM dual;
select SUBSTR (
'imagelocation/r1.jpg',
INSTR('imagelocation/r1.jpg', '/')+1,
LENGTH('imagelocation/r1.jpg') - INSTR('imagelocation/r1.jpg', '.') - 1
) AS image_location
FROM dual
SUBSTR Function in Oracle
SQL Fiddle
Try this:
SELECT SUBSTR('imagelocation/r1.jpg',15,2) AS image_location FROM dual

Removing special character SQL

I have column in a table which contains special characters with an attached string. The same column contains numbers also. I am only intresed in extracting numbers out of that column e.g
Name-3445 => 3445; Out-90 => 90; 786 => 786
How would i do this in SQL or PL/SQL?
SELECT regexp_replace(some_column, '[^0-9]*', '') as clean_value
FROM your_table
PL/SQL has a REGEX_REPLACE function, which you could use to replace anything that isn't a digit with an empty string. Details on REGEX_REPLACE can be found here: http://psoug.org/reference/regexp.html
Without knowing the integrity of your data, something like this might do what you're asking:
select CAST(SUBSTRING(_COLUMNNAME_,CHARINDEX('-', _COLUMNNAME_),1000), Integer) as ColumnName
from tblTable where _COLUMNNAME_ like '%-%'
union all select CAST(_COLUMNNAME, Integer) as ColumnName
from tblTable where _COLUMNNAME_ not like '%-%'
You can use the regexp_substr function:
http://docs.oracle.com/cd/B14117_01/server.101/b10759/functions116.htm
REGEXP_REPLACE(<Your_String>,'[^[:alnum:]'' '']', NULL)
Example:
SELECT REGEXP_REPLACE('##$$$123&&!!__!','[^[:alnum:]'' '']', NULL) FROM dual;
Output:
123

Extract text before third - "Dash" in SQL

Can you please help to get this code for SQL?
I have column name INFO_01 which contain info like:
D10-52247-479-245 HALL SO
and I would like to extract only
D10-52247-479
I want the part of the text before the third "-" dash.
You'll need to get the position of the third dash (using instr) and then use substr to get the necessary part of the string.
with temp as (
select 'D10-52247-479-245 HALL SO' test_string from dual)
select test_string,
instr(test_string,1,3) third_dash,
substr(test_string,1,instr(test_string,1,3)-1) result
from temp
);
Here is a simple statement that should work:
SELECT SUBSTR(column, 1, INSTR(column,'-',1,3) ) FROM table;
Using a combination of SUBSTR and INSTR will return what you want:
SELECT SUBSTR('D10-52247-479-245', 0, INSTR('D10-52247-479-245', '-', -1, 1)-1) AS output
FROM DUAL
Result:
output
-------------
D10-52247-479
Use:
SELECT SUBSTR(t.column, 0, INSTR(t.column, '-', -1, 1)-1) AS output
FROM YOUR_TABLE t
Reference:
SUBSTR
INSTR
Addendum
If using Oracle10g+, you can use regex via REGEXP_SUBSTR.
I'm assuming MySQL, let me know if I'm wrong here. But using SUBSTRING_INDEX you could do the following:
SELECT SUBSTRING_INDEX(column, '-', 3)
EDIT
Appears to be oracle. Looks like we may have to resort to REGEXP_SUBSTR
SELECT REGEXP_SUBSTR(column, '^((?.*\-){2}[^\-]*)')
Can't test, so not sure what kind of result that will have...