I need to pull a substring from a string where there are multiple same delimiters. Has to be done in a sql query (no function calls, etc.)
Example of data:
DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS
PA:TIER4:VQ:SPI:H:SID005000009V3VO7:STPACPB3:CI
The data I need is between the last and second to last colons.
Data:
FDCADIND
STPACPB3
Data will always be in the same position. Last three characters will always be format of :XX
I started on a query but can't seem to get it to reverse correctly.
Select SubString('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', (CHARINDEX(':', 'DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', 0) + 1),
(CharIndex(':', RIGHT('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', (LEN('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS') - (CharIndex(':', 'DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', 0)))), 0) - 1)) As NewString
Any help appreciated,
Chris
If the data (last two fields) are of fixed length:
SELECT LEFT(RIGHT('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS',11),8)
Related
I am working on a problem where I would like to extract all parts of a string before the last instance of a repeating character.
I have the below data as an example:
string_variable
string_variable_two
My goal is to extract all text in the string before the last underscore "_".
So for the above I would end with:
string
string_variable
I cannot split because in some instances I have a single, and in some I have two. I also tried using a REGEX_EXTRACT but BigQuery will not allow look ahead functions for .*(?=_)
Consider below
select str,
ifnull(safe.left(str, instr(str, '_', -1) - 1), str)
from your_table
if applied to sample data in y our question - output is
I'm querying a dataset using Oracle SQL Developer and want to create a column with partial return data from a (CLOB) in another column.
The part I need is in quotes and i've used substring to extract however the value will change as will the length of the string. Is there a way to end when reach closing quotes instead of specifying length of string?
dbms_lob.substr(a.LINETESTRESULT,15,dbms_lob.instr(UPPER(a.LINETESTRESULT),'LHCRAG')+11) AS REFRESH_RAG
At the minute, 15 characters are returned, but the latest additions are shorter and showing the ", from the next entry. I need to replace the length attribute.
The result I get is:
Red_Session",
I need the output to be: Red_Session
or whatever the return value is, I just need it to end before the closing quote.
INSTR has a parameter nth (Occurrence number, starting at 1.). With this you can create something like this.
dbms_lob.substr(a.LINETESTRESULT,dbms_lob.instr(a.LINETESTRESULT, '"', 1, 2) - dbms_lob.instr(a.LINETESTRESULT, '"') - 1, dbms_lob.instr(a.LINETESTRESULT, '"') + 1) AS REFRESH_RAG
you might try REGEXP_SUBSTR like below. Alternatively you can find the position of the quote and use the substr...
SELECT
REGEXP_SUBSTR(a.LINETESTRESULT,
'"([^"]*)') AS REFRESH_RAG
FROM DUAL;
Managed to get this using the following code:
SELECT
dbms_lob.substr(UPPER(a.LINETESTRESULT), dbms_lob.instr(UPPER(a.LINETESTRESULT), '"',
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11) -
(dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11),
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11)
AS REFRESH_RAG
I want to use regex through sql to query some data to return values. The only valid values below returned would be "GB" and "LDN", or could also be "GB-LDN"
G-GB-LDN-TT-TEST
G-GB-LDNN-TT-TEST
G-GBS-LDN-TT-TEST
As it writes the first GB set needs to have 2 characters specifically, and the LDN needs to have 3 characters specifically. Both sets/groups seperated by an - symbol. I kind of need to extract the data but at the same time ensure it is within that pattern. I took a look at regex but I can't see how to, well it's like substring but I can't see it.
IF i undertsand correctly, you could still use of substring() function to extract the string parts separated by -.
select left(parsename(a.string, 3), 2) +'-'+ left(parsename(a.string, 2) ,3) from
(
select replace(substring(data, 1, len(data)-charindex('-', reverse(data))), '-', '.') [string] from <table>
) a
As in above you could also define the length of extracted string.
Result :
GB-LDN
GB-LDN
GB-LDN
How to trim values which end with '?
Example: I have more than 100k rows with column report name like report[#name='Air Bookings by Status Code']
The actual report name is just
Air Bookings by Status Code
How do I trim this string?
I tried doing
substr(a.COGIPF_REPORTNAME, INSTR(a.COGIPF_REPORTNAME, '=', 1,1)+2, INSTR(a.COGIPF_REPORTNAME, ']')-2)
I'm able to trip the first part. How do I do the latter part?
You can use regexp_substr:
regexp_substr(a.COGIPF_REPORTNAME,q'<'([^']*)'>',1,1,null,1)
If you have a version wihtout regex, you can also use "pure" SQL functions:
SUBSTR(a.COGIPF_REPORTNAME,
INSTR(a.COGIPF_REPORTNAME, '=') + 2,
LENGTH(a.COGIPF_REPORTNAME) - INSTR(a.COGIPF_REPORTNAME, '=') - 3)
Note that the second parameter to SUBSTRING is the length of the desired substring.
This works if it is known, that the text always terminates with "']". Otherwise you will have to calculate the length differently:
SUBSTR(a.COGIPF_REPORTNAME,
INSTR(a.COGIPF_REPORTNAME, '=') + 2,
INSTR(a.COGIPF_REPORTNAME, ']') - INSTR(a.COGIPF_REPORTNAME, '=') - 3)
Why subtracting 3? Well - we have to subtract 2 for the two single quotes, but even if the text is empty, the position of the last single quote will be one more than that of the first single quote. Since an empty text has a length of 0, we must subtract one more.
See: http://sqlfiddle.com/#!4/32454a/10
In my table for the rows containing values like
sample>test Y10,
Sample> y21
I want to get a substring like y10,y21 from all rows. May I pls know how to get it. I tried with regexp_substr,Instr but not able to find the solution.
I am supposing that your string from column is devided by a single space .
It will give you last occurances which will be splited by ' ' a space
substr(your_string, 1, instr(yourString,' ') - 1)
OR you can achive this using regexp_substr
regexp_substr(your_String, '[^[:space:]]+', 1, -1 )
Assuming that yxx is always preceded by a space, it should be as easy as doing this:
TRIM(REGEXP_SUBSTR(mycolumn, ' y\d+', 1, 1, 'i'))
The above regular expression will grab y (note that it is case-insensitive, so it will grab Y as well) followed by an indefinite number (one or more) of digits. If you want to grab just two digits, replace \d+ with \d{2}.
Also, please note that it will get the first occurrence only. Getting multiple occurrences is a bit more complicated, but it can still be done.