substr in Oracle from column - sql

What is the Syntax to substr in Oracle to subtract a string
i have "123456789 #073"
I only want what after the #
substr (table.col, 17,3)
is that ok ?

Most likely the simplest (and most performant) way of doing this would be to use the base string functions:
SELECT SUBSTR(col, INSTR(col, '#') + 1)
FROM yourTable;
Demo
We could also try using REGEXP_REPLACE here:
SELECT REGEXP_REPLACE(col, '.*#(.*)', '\1')
FROM yourTable;
The regex option would in general not perform as well as the first query. The reason for this is that invoking a regex incurs a performance overhead. You might want to consider a regex option if you expect that the string logic might change or get more complicated in the future. Otherwise, go with base string functions wherever possible.

I think the most direct method might be regexp_substr():
select regexp_substr('123456789 #073', '[^#]+$')
from dual;
The regular expression says: "get me all non-hash characters at the end of the string".
If you happen to know that there are 3 characters and really want the last three characters of the string:
select substr('123456789 #073', -3)

Related

Redshift SQL REGEXP_REPLACE function

I have a value which is duplicated from source (can't do anything about that). I have read some examples here https://docs.aws.amazon.com/redshift/latest/dg/REGEXP_REPLACE.html
Example value:
ABC$ABC$
So just trimming anything after the first '€'. I tried this, but I cannot figure out the correct REGEX expression.
REGEXP_REPLACE(value, '€.*\\.$', '')
So just trimming anything after the first '€'.
Why use regex at all? Why not just..
SELECT LEFT(value, CHARINDEX('€', value)-1)
If not all your data has a euro sign, consider WHERE value like '%€%'
Your current regex pattern is including a dot as the final character. Remove it and your approach should work:
SELECT REGEXP_REPLACE(value, '€.*$', '') AS value_out
FROM yourTable;
Or you can take the initial sequence of non-€ characters:
REGEXP_SUBSTR(value, '^[^€]+')

Query to take the value after '/'

Suppose there is a value 842545/003. I need to take the part after '/'.
84454/02. I want a query to take the only 02 from here
You can try below substr() and instr() function
select substr('84454/02',instr('84454/02','/')+1,
length('84454/02')-instr('84454/02','/')) as val
from dual
You can use a regex (with the usual warnings about regex performance - the simple string functions like instr and substr are faster if you are processing millions of rows).
regexp_replace(yourcolumn, '^.*/')
This removes everything up to and including the / character (or the final one if there is more than one).
If your are using SQL, you can use this.
SELECT SUBSTRING('84454/02',PATINDEX('%/%', '84454/02')+1,LEN('84454/02'));
'84454/02'= Column name

Reverse part of string

by using SQL function 'reverse', do we reverse a particular part in a string without changing the remaining characters .. [i.e praCTIce to praITCce];
thank you for replying;
Your question is very unclear, but assume you want to reverse first 3 character long uppercase string occurence so you can use:
SqlFiddleDemo
WITH cte AS
(
SELECT 'praCTIce' AS col FROM dual
)
SELECT
col AS original,
REPLACE(col,
REGEXP_SUBSTR(col, '[A-Z]{3}'),
REVERSE(REGEXP_SUBSTR(col, '[A-Z]{3}'))) AS result
FROM cte;
You just need to REPLACE one substring with reversed substring.
Warning
Keep in mind that provided solution is not general, it is just demo/clue how to tackle problem.
You can enhance the REVERSE function using the SUBSTRING or REGEXP class functions. The single REVERSE just reverses entire string.

Cut string after first occurrence of a character

I have strings like 'keepme:cutme' or 'string-without-separator' which should become respectively 'keepme' and 'string-without-separator'. Can this be done in PostgreSQL? I tried:
select substring('first:last' from '.+:')
But this leaves the : in and won't work if there is no : in the string.
Use split_part():
SELECT split_part('first:last', ':', 1) AS first_part
Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc.
Substantially faster than functions using regular expression matching. And since we have a fixed delimiter we don't need the magic of regular expressions.
Related:
Split comma separated column data into additional columns
regexp_replace() may be overload for what you need, but it also gives the additional benefit of regex. For instance, if strings use multiple delimiters.
Example use:
select regexp_replace( 'first:last', E':.*', '');
SQL Select to pick everything after the last occurrence of a character
select right('first:last', charindex(':', reverse('first:last')) - 1)

REGEXP_SUBSTR to extract fixed length string starting from a digit

Table A
ID ID_Descr
1 'DUP 8002061286'
2 'DUP 8002082667 '
3 ' 8002082669 DUP'
I would like to extract the string from the ID_Descr field with the following conditions:
String always starts with 8
String length is always 10 digits
This means stripping everything to the right and left of the string (eg. '8002082669'). How can I achieve this? Using REGEXP_SUBSTR?
I am using Oracle 11g.
Thanks!
Although you could use regexp_substr() for this, I would take a different approach. I would just look for the '8' using instr() and then take the next 10 characters:
select substr(id_descr, instr(id_descr, '8'), 10)
This seems like the simplest solution.
You could use REGEXP_SUBSTR() but a regex is an expensive operation so you would be much better off using SUBSTR() and INSTR():
SELECT SUBSTR(ID_Descr, INSTR(ID_Descr, '8'), 10) FROM tableA;
If you really wanted to use REGEXP_SUBSTR() you could do it as follows:
SELECT REGEXP_SUBSTR(ID_Descr, '8.{9}') FROM tableA;
This would get 8 plus the following 9 characters (. being the wildcard).
Now if you wanted to match only digits, then REGEXP_SUBSTR() would probably be your best bet:
SELECT REGEXP_SUBSTR(ID_Descr, '8[0-9]{9}') FROM tableA;