How do I trim content to get desired string in oracle - sql

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

Related

Remove numbers from beginning of strings in a numbered list with SQLite

I have a long numbered list imported into a table, the strings are in the following format:
1. fdhsglahs sdhkgs
2. urgbvdgh ndovh
3. 8yhbnxjghr nvdfo dfhioj
...
9999. vnur neeu nu
I want to remove the numbers in the beginning of the string, the "." adjacent to the number, and any number of spaces that come immediately after the "." and before the next character (that is, before the beginning of the string itself).
Can't find a method to do that in SQLite.
Please notice, some of the strings contain numbers as part of the string, which are not to be removed.
For this requirement you can use string functions like substr(), instr() and ltrim():
select ltrim(substr(col, instr(col, '.') + 1))
from tablename
Replace col with the column's name.
this code returns the part of the string after the . left trimmed of spaces.
See the demo.
If you want to update the table:
update tablename
set col = ltrim(substr(col, instr(col, '.') + 1));
See the demo.

How to return string within quotes

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

Parsing a string and comparing values to existing column

I have the below table with the string marked "Remark" that needs to be parsed. The highlighted fares need to be compared from the columns TotalBookedFare and Remark. The only issue is that the value I need to compare under the Remark column is in the middle of a string. I've tried to parse the string but I cannot figure it out. I am using SQL Server 2008. As you can see the first row is not a match while the other three are matching.
Ideally I would like to convert the one string "Remark" to the 5 columns listed below so I can compare the TotalBookedFare to the "New" column.dionbennett
I think this should work
select substring(
remark, --string base
charindex ('/', 'xyz/57.77usd/zyx') + 1,
--starting position is location one to the right of first instance of / character (5)
charindex ('u', 'xyz/57.77usd/zyx', charindex ('/', 'xyz/57.77usd/zyx')) - charindex ('/', 'xyz/57.77usd/zyx') - 1
--length is the location of the first instance of the u character
--starting from the location of first instance of the / character (10)
--then subtracted by the location of the first instance of the / character (4)
--and then an additional 1 resulting in the length of the string to be extracted (5)
)
The string I put in there is just a more concrete example, if you replace it with Remark, it should extract the substring for each row. You could even modify it with some copy/pasting to get each of those columns you were looking for.

SQL / REGEX pattern matching

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 get regexp_substr for a string

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.