i'm trying to retrieve the last substring from a string, starting from the end.
Here it is my dataset:
Input:
BRAND_Arnette
BRAND_Persol
MODEL_CODE_DISPLAY_226781
Output:
Arnette
Persol
226781
What i 've managed to do is to retrieve what i need, but i'm not using an universal approach, because i'm considerging always the latest 10 chars, starting from the right:
SELECT
SUBSTR(RIGHT(rtrim(cast(attrval.IDENTIFIER as char(50))), 10), LOCATE('_',RIGHT(rtrim(cast(attrval.IDENTIFIER as char(50))), 10))+1)
FROM ...
How can this select be edited so it can be always valid? Thanks
Try the following expression:
SUBSTR (identifier, LOCATE_IN_STRING (identifier, '_', -1) + 1)
dbfiddle example.
I would suggest regexp_substr():
select regexp_substr(identifier, '[^_]+$')
Here is a db<>fiddle.
Related
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
I want to return all characters before the 2nd occurrence of the character slash '/' (if any) in PostgreSQL.
Input Column:
/apple/orange/banana
/
/mango
/avocado/kiwi
Desired Output Column:
/apple
/
/mango
/avocado
Can anyone help with this please?
One method is regexp_replace():
select t.*,
regexp_replace(col, '^([^/]*/[^/]*)/.*$', '\1')
from t;
Here is a db<>fiddle.
You can use substring() with a regex:
select substring(the_column from '(/\w*)')
from the_table
Another alternative would be split_part()
select '/'||split_part(the_column, '/', 2)
from data
i would make a script that iterate over the records of a table with a cursor
and extract from a column value formatted like that "yyy://xx/bb/147011"
only the final number 147011and to put this value in a variable.
It's possible to do something like that?
Many thanks.
You don't need a cursor for this. You can just use a query. The following gets everything after the last /:
select right(str, charindex('/', reverse(str)) - 1 )
from (values ('yyy://xx/bb/147011')) v(str)
It does not specifically check if it is a number, but that can be added as well.
You can also use the below query.
SELECT RIGHT(RTRIM('yyy://xx/bb/147011'),
CHARINDEX('/', REVERSE('/' + RTRIM('yyy://xx/bb/147011'))) - 1) AS LastWord
If numeric value has exact position defined with sample data, then you can do :
SELECT t.*, SUBSTRING(t.col, PATINDEX('%[0-9]%', t.col), LEN(t.col))
FROM table t;
I have emails like sales#joebloggs.com.
I am looking to select out everything after the # and before the ..
Result should be joebloggs.
Simple regex pattern should do the job.
SELECT regexp_matches('sales#joebloggs.subdomain.com', '#([^.]+)\.');
SQL Fiddle
Edit: fixed to support subdomains. I assume you want to get the part before first dot.
Here is a solution using substring and position:
substring(col from (position('#' in col)+1)
for (
position('.' in substring(col from (position('#' in col)+1))) - 1
)
)
Solution using substring and instr
SELECT
substring('sales#joebloggs.com', instr('sales#joebloggs.com', '#') + 1, instr('sales#joebloggs.com', '.') - (instr('sales#joebloggs.com', '#') + 1))
FROM dual;
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...