How do I extract a substring starting from end of string until the second 0 is encountered, oracle? - sql

I have a table client_requests with the column number_request.
In the column number_request I have the following records:
20130000000008,
20130000000010,
20130000000503
I want to extract only the end of string without 0,
example:
for 20130000000008 I want to get 8
for 20130000000010 I want to get 10
for 20130000000503 I want to get 503
I think I need to use regexp_substr but do not know how.

One possible solution:
select replace('201500000010',regexp_substr('201500000010','.{4}0*'),'') from dual;
Essentially, I'm using the regexp_subst function to extract the unwanted digits, then using the replace function to replace the unwanted digits with '', which is nothing.

Related

How to extract first 5 characters from filename in LogicApps

I have an issue, could you help me to understand how to extract first 5 characters from the filename in LogicApps. I'm able to create a variable and append it, so I have the list with file names, but then struggling to extract specific characters...
You can also use the take expression:
take(variables('my_string'),5)
This also work for arrays if you want to return the 5 first array items.
Use the substring expression ...
https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#substring
substring('<text>', <startIndex>, <length>)
The expression within the First 5 Characters variable for the below result looks like this ...
substring(variables('String Variable'), 0, 5)
Result

Using the function SPLIT_PART twice in PostgreSQL?

I have a TEXT column where each text is formatted as such:
/customers/{customer_id}/views/{id1}~{id2}/
I am trying to fetch the id2 only.
My idea is how to split the string by the / character first, where I will have:
customers, {customer_id}, views, {id1}~{id2}.
And then, get the last position:
{id1}~{id2}. And then split it again by the ~ character, and finally get the last position.
The issue is that I am new to SQL and I have no idea if this is even possible. How can I do that and end up with only one column?
SELECT
split_part(thetext, '/', 4) as temp
// how do I proceed from here?
FROM mytable
EDIT:
Some examples:
/customers/1231341/views/1312391293~3432491/
/customers/2213441/views/424131~231321341/
The IDs are of different lengths as well.
Use regexp_replace() to capture the part you want while matching the whole input, and replacing (the whole input) with the capture:
select regexp_replace(thetext, '.*~(.*)/', '\1') as temp
from mytable
See live demo.

split string with character

using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder

Oracle: remove first 4 characters from a string

So I want to remove the first 4 characters from a string in oracle. Those characters can be different every time.
In my case I need to take away the first 4 characters of an IBAN and put them at the end of the string. I got the part of putting them to the end of the string but I can't get the first 4 characters to be removed. Every solution I find on the internet removes specified characters, not characters from a certain position in the string (1 to 4).
I used the code below to get the first 4 characters to the end of the string and wanted to try something similar for removing them at the front but without success.
SELECT SUBSTR(iban_nummer, 1, 4) INTO iban_substring FROM dual;
iban_nummer := iban_nummer || iban_substring;
See the docs:
substring_length ...
When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify
a value that is less than 1, the function returns NA.
So iban_nummer := substr(iban_nummer, 5) || substr(iban_nummer, 1,4) should work. The first part selects all characters beginning from the 5th, the second character numbers 1..4.
update table_name set col_name=substr(col_name,5);
try regexp, like:
SELECT regexp_replace(t.iban_nummer,'(.{4})(.*)','\2\1') FROM t;
Alternative way using regexp :
SELECT regexp_replace(t.iban_nummer,'^.{4}(.*)','\2\1') FROM dual;

How to extract group from regular expression in Oracle?

I got this query and want to extract the value between the brackets.
select de_desc, regexp_substr(de_desc, '\[(.+)\]', 1)
from DATABASE
where col_name like '[%]';
It however gives me the value with the brackets such as "[TEST]". I just want "TEST". How do I modify the query to get it?
The third parameter of the REGEXP_SUBSTR function indicates the position in the target string (de_desc in your example) where you want to start searching. Assuming a match is found in the given portion of the string, it doesn't affect what is returned.
In Oracle 11g, there is a sixth parameter to the function, that I think is what you are trying to use, which indicates the capture group that you want returned. An example of proper use would be:
SELECT regexp_substr('abc[def]ghi', '\[(.+)\]', 1,1,NULL,1) from dual;
Where the last parameter 1 indicate the number of the capture group you want returned. Here is a link to the documentation that describes the parameter.
10g does not appear to have this option, but in your case you can achieve the same result with:
select substr( match, 2, length(match)-2 ) from (
SELECT regexp_substr('abc[def]ghi', '\[(.+)\]') match FROM dual
);
since you know that a match will have exactly one excess character at the beginning and end. (Alternatively, you could use RTRIM and LTRIM to remove brackets from both ends of the result.)
You need to do a replace and use a regex pattern that matches the whole string.
select regexp_replace(de_desc, '.*\[(.+)\].*', '\1') from DATABASE;